I've an array of objects and I want to modify a property in the object.
I know we can remove the object using splice function.Is the only option to remove and add it back?
I've an array of objects and I want to modify a property in the object.
I know we can remove the object using splice function.Is the only option to remove and add it back?
Share Improve this question edited Jul 14, 2017 at 20:36 Kuf 17.8k7 gold badges68 silver badges91 bronze badges asked Feb 19, 2016 at 7:42 bharz629bharz629 1612 gold badges4 silver badges10 bronze badges 1-
4
no, you can directly change it ...
array[index].property = value
... or, as the question implies, to replace an object ...array[index] = newobject
– Jaromanda X Commented Feb 19, 2016 at 7:45
2 Answers
Reset to default 4You have to modify it directly (assuming You know index)
YourArray[index].ObjectProperty = YourValue
It does not make any difference if it's ES6 or not
If all you need to do is modify a prop in the object, theres no need to remove it from the array.
arr[2].prop = newValue;