简体   繁体   中英

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 and 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]] = [];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM