简体   繁体   中英

How to insert a JS object into an anonymous array

I have an object:

var JSONObject = {
    "thing": {
        "array": [{
            "a": "aaa"
        }]
    }
};

And I'm trying to push another object here:

var JSONObject = {
    "thing": {
        "array": [{
            "a": "aaa",
            "NEW THING": "GOES HERE"
        }]
    }
};

Normally, you would do:

JSONObject.thing.array.push({"NEW THING": "GOES HERE"})

But in this case I can't do that, since there's no real handle to the object; it's undefined.

Any ideas would be greatly appreciated!

Thanks.

Do

JSONObject.thing.array[0]["NEW THING"] = "GOES HERE";

This

{
   "a": "aaa"
}

is not an array, but an object and you want to insert a new key value pair.

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