简体   繁体   中英

Add key value to an object

I want to push a key-value pair in the 1st element

Like this

 updatedPlatingProcs = [{
    "data":{
     "AsfTime":1,
     "newKey":"newValue" -- // this is where I wanna add the new key-value pair
    },
    "data2":{
     "Asf":3
    }
}]

I tried something like this

       var platingTimeVal = {
           PlatingTime: processArea * time,
       };
       updatedPlatingProcs[0]["data"].push(platingTimeVal);
       updatedPlatingProcs.push(platingTimeVal); // also this

Got wrong results

Your error is that data in updatedPlatingProcs[0]["data"] is not an array, it is an object.

You can set a value of that object like so:

updatedPlatingProcs[0].data.PlatingTime = processArea * time;

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