开发者

jQuery iterate over object with $.each

I have an object options: options = {title : 'title1', name : 'name1', url : 'url1', etc.}

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 ob开发者_如何学JAVAject 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)}

instead of

{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.

(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);

To use a dynamic key use []:

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

Specific example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜