开发者

Javascript Dynamic Arrays and Object

Is this possible?

So I need to have an array with a dynamic name and content what can be extended 开发者_如何学Goand accessed.

object = {};
var two = ['thing', 'thing2'];
for(one in two){
    object[two[one]] = [];
}

If yes but not in this way, then how?


This is definitely doable, just make sure that the object owns the property and it's not inherited from higher up in the prototype chain:

object = {};
var two = ['thing', 'thing2'];

for..in:

for(var one in two){
    if(two.hasOwnProperty(one))
       object[two[one]] = [];
}

for:

for(var i = 0; i < two.length; i++)
   object[two[i]] = [];


var object = {};
var props  = 'thing thing2'.split(' ');
for (var i=0,len=props.length;i<len;++i){
  object[props[i]] = [];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜