Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
90
rated 0 times [  96] [ 6]  / answers: 1 / hits: 81712  / 12 Years ago, thu, april 12, 2012, 12:00:00

Suppose I have several javascript object



{type:gotopage,target:undefined}
{type:press,target:a}
{type:rotate,target:long}


How can I add this objects to another object like



config={}


I know how to if each inserted object have a id I can added as:



config[id]={}


But in this case how can I add objects without id?


More From » object

 Answers
7

I think you are mixing up objects and arrays. Objects have named keys, arrays have numeric keys. You can easily append to an array using .push():



var arr = [];
arr.push(something);


However, for a configuration object as your variable name suggests this is not really useful. You should rather use meaningful names for your options, e.g.:



var config = {
something: 'blah',
foo: 'bar'
};


You can also store an array in your object:



config.manyStuff = [];
for(...) {
manyStuff.push(...);
}

[#86294] Wednesday, April 11, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anikas

Total Points: 258
Total Questions: 102
Total Answers: 95

Location: Monaco
Member since Sun, Jan 16, 2022
2 Years ago
;