jQuery的迭代与$。每个对象 [英] jQuery iterate over object with $.each

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

问题描述

我有一个对象选项选项= {标题:TITLE1',名称:名1,网址:'为url1,等等}

这是传过来的参数的函数。我试图遍历该对象,传递给另一个函数评估,并将结果存储在另一个对象选择采用,就像这样:

which is passed in as a parameter to a function. I'm trying to iterate over that object, pass it through another function evaluate, and store the result in another object opts, like so:

var opts = new Object();

$.each(options, function(key,value){
opts.key = evaluate(element, value);
});

评估(元素值)工作正常,但问题是,选择采用结束看起来像:

The evaluate(element,value) works fine, but the problem is that opts ends up looking like:

{key : eval(element,url1)}

而不是

{title : eval(element,title1), name : eval(element,name1), etc.}

这就是被逐字传递,而不是评估,并得到与最后一个属性每次迭代中的选项覆盖

That is, key gets passed literally instead of being evaluated, and gets overwritten with each iteration with the last property in options.

我必须在我行指定正确的语法?我也试过:

Do I have the right syntax in my assignment line? I also tried:

opts = {key : eval(element,val)}

这给了上述相同的结果。我还可以将对象转换为数组中的 $范围内。每个迭代。我试着这样做的几种方法,也没有成功。如果有人能告诉我这条路线,这将是巨大的。

which gave the same result as above. I can also convert the object to an array within the $.each iteration. I've tried several ways of doing that, also unsuccessfully. If someone can show me that route, that would be great too.

(这是一个jQuery插件,而且我在Firefox使用萤火虫测试)。

(This is for a jQuery plugin, and I'm testing using Firebug in Firefox).

在此先感谢您的帮助。

Thanks in advance for your help.

推荐答案

当你已经发现这是字面上的关键钥匙:

As you've discovered this is the literal key 'key':

opts.key = evaluate(element, value);

要使用动态密钥使用 []

opts[key] = evaluate(element, value);

具体的例子:

var o = {};
o["test"] = "foo";
alert(o.test);

这篇关于jQuery的迭代与$。每个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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