I have a function that needs to extend a javascript array, including a new attribute called selected
:
export const initSelect = (data) => { let newData = data.concat(); newData.map((item) => { item.selected = false; }) return newData; }
data
is a ReactJS state value (comes from this.state.data
when calling the function), but this didn't seem to be a problem as newData
is a new copy of data
array...
I'm getting the following error:
TypeError: Cannot add property selected, object is not extensible
To fix this error, you will either need to remove the call to Object. preventExtensions() entirely, or move it to a position so that the property is added earlier and only later the object is marked as non-extensible. Of course you can also remove the property that was attempted to be added, if you don't need it.
Built-in classes like Array, Map and others are extendable also.
You probably need to copy the objects:
export const initSelect = (data) => { return data.map((item) => ({ ...item, selected: false })); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With