开发者

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

开发者 https://www.devze.com 2023-03-15 17:27 出处:网络
var adddata = {}; adddata[ group ] = {}; adddata[ group ].type = type; adddata[ group ].items = {}; adddata[ group ].items[ id ] = value;
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

精彩评论

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

关注公众号