0

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

asked Jul 20, 2021 at 6:45
1
  • 1
    updatedPlatingProcs[0]["data"] is an Object, not an Array. You can not use the push method on an Object. This updatedPlatingProcs.push(platingTimeVal); should actually work. Commented Jul 20, 2021 at 6:47

1 Answer 1

4

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;
answered Jul 20, 2021 at 6:48
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.