Question:
Hi trying to add a new object to the list of cars for particular user, below is my array of object called inventory. Ive used a find to get the user id which return.I want to add extra objects to the cars property i.e {model: “Porsche”,year: “2009”} to the user array when the user id = 1
Is there a cleaner way of doing this without using push
Answer:
You can use Array.prototype.map and update the item that has anid
of 1
.If you want don’t want to create a new array, then you can use Array.prototype.forEach instead of
map
.If you have better answer, please add a comment about this, thank you!