在为对象数组创建新属性时,对象不是可扩展的错误 [英] Object is not extensible error when creating new attribute for array of objects
本文介绍了在为对象数组创建新属性时,对象不是可扩展的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个需要扩展javascript数组的函数,包括一个名为的新属性
:
I have a function that needs to extend an javascript array, including a new attribute called selected
:
export const initSelect = (data) => {
let newData = data.concat();
newData.map((item) => {
item.selected = false;
})
return newData;
}
数据
是一个ReactJS状态值(来自 this.state.data
调用函数时),但这不会出现问题 newData
是数据的新副本
数组...
data
is a ReactJS state value (comes from this.state.data
when calling the function), but this not seen to be a problem as newData
is a new copy of data
array...
我收到以下错误:
TypeError: Cannot add property selected, object is not extensible
推荐答案
您可能需要复制对象:
export const initSelect = (data) => {
return data.map((item) => ({
...item,
selected: false
}));
}
这篇关于在为对象数组创建新属性时,对象不是可扩展的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文