开发者

Is there a shorter way to define an object structure with variable keys?

var adddata = {};
adddata[ group ] = {};
adddata[ group ].type = type;
adddata[ group ].items = {};
adddata[ group ].items[ id ] = value;
$.extend( data, adddata );

Do i really need to define adddata this long way? If group and id are fixed strings, it's pretty short

$.extend( data, { group1:{ type: t开发者_如何转开发ype, items:{ id1: value } } } );


Javascript only supports literal keys within object literals. You can only make the code a little shorter:

var adddata = {};
adddata[group] = { type: type, items: {} };
adddata[group].items[id] = value;
$.extend(data, adddata);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜