简体   繁体   English

如何更新es6数组中的元素?

[英]how to update an element in a es6 array?

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? 我知道我们可以使用splice函数删除对象。这是删除并添加回来的唯一选项吗?

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 如果它是ES6,它没有任何区别

如果您只需要修改对象中的prop,则无需将其从数组中删除。

arr[2].prop = newValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM