Javascript:迭代对象文字值 [英] Javascript: Iterate over Object Literal Values

查看:92
本文介绍了Javascript:迭代对象文字值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的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

我正在尝试循环使用默认变量。对于找到的每个对象(来自 numberOfObjects ),我需要复制变量值的值。因此,如果 numberOfObjects 变量为3,则 defaults.starts 应为 0px,0px > 0px,0px> 0像素,0像素。 >用来分割价值。

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 > 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:

Or with jQuery:

$.each(obj, function(key, value) {
    console.log(this, value, obj[key]);
});

这篇关于Javascript:迭代对象文字值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆