javascript - how to update an element in a es6 array? - Stack Overflow

admin2025-04-19  1

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
Add a ment  | 

2 Answers 2

Reset to default 4

You 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;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745002136a279293.html

最新回复(0)