遍历对象文字值 [英] Iterate over Object Literal Values
问题描述
我有一个标准的jQuery插件,可在顶部创建默认设置:
I have a standard jQuery plugin set up that creates default settings at the top:
jQuery.fn.myPlugin = function(options) {
var defaults = {
starts: "0px,0px",
speed: 250, ...
};
o = $.extend(defaults, options);
}
我还有一个名为numberOfObjects
的变量.
I have another variable called numberOfObjects
.
我正在尝试遍历默认变量.对于找到的每个对象(从numberOfObjects
),我需要复制变量值的值.因此,如果numberOfObjects
变量为3,则defaults.starts
应该为0px,0px > 0px,0px > 0px,0px
. & gt;用于拆分值.
I'm trying to loop through the default variables. For each object found (from numberOfObjects
) I need to duplicate the value of the variable value. So, if the numberOfObjects
variable is 3, the defaults.starts
should be 0px,0px > 0px,0px > 0px,0px
. The & gt; is used to split values.
这是我目前拥有的. X
代表默认值内的变量名. Y
表示x
当前值的变量.我已经走了这么远,不知道下一步该怎么做.
This is what I currently have. X
represents the variable name inside defaults. Y
represents the variable for the current value of x
. I've gotten this far and have no clue what to do next.
for (x in defaults) { // x is defaults.x
defaults.x = defaults.x + " > y";
}
推荐答案
var obj = {
'foo': 1,
'bar': 2
};
for (var key in obj) {
console.log(obj[key]);
}
或使用jQuery:
$.each(obj, function(key, value) {
console.log(this, value, obj[key]);
});
这篇关于遍历对象文字值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!