Search code examples
javascriptobjectaddition

How can I add javascript object to another object in dynamic


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?


Solution

  • 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(...);
    }