Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - push into JSON array

Just trying to update a JSON array and hoping for some guidance.

var updatedData = { updatedValues: [{a:0,b:0}]};    
updatedData.updatedValues.push({c:0}); 

That will give me:

{updatedValues: [{a: 0, b: 0}, {c: 0}]}

How can I make it so that "c" is part of that original array? So I end up with {a: 0, b: 0, c: 0} in updatedValues?

like image 758
b85411 Avatar asked Aug 07 '26 16:08

b85411


1 Answers

You actually have an object inside your array.

updatedData.updatedValues[0].c = 0; 

will result in your desired outcome.

like image 193
TimoStaudinger Avatar answered Aug 10 '26 06:08

TimoStaudinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!