asdf
asdf

Reputation: 33

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

Upvotes: 0

Views: 817

Answers (1)

coagmano
coagmano

Reputation: 5671

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;

Upvotes: 4

Related Questions