I have a vector of objects in JavaScript (React Native) and I am trying to assign a unique random key to each item in the array. However, when I add the key property to one object, it ends up being the same for every element in the array.
It's puzzling me as to why this issue is occurring.
The original array looks like this:
[{"nome":"insalata di mare","prezzo":0.3,"qr_code":"qr_valore","quantita":3},{"nome":"insalata di mare","prezzo":0.3,"qr_code":"qr_valore","quantita":3}]
The resulting array is as follows:
Random product list: [{"nome":"insalata di mare","prezzo":0.3,"qr_code":"qr_valore","quantita":3,"key":"x4idec"},{"nome":"insalata di mare","prezzo":0.3,"qr_code":"qr_valore","quantita":3,"key":"x4idec"}]
This issue seems to occur only when the items are identical.
componentWillMount(){
this.props.productList.map(item => {
item.key = Math.random().toString(36).substring(7)
})
}
The desired outcome should be a different key assigned to each object, something like this:
Random product list: [{"nome":"insalata di mare","prezzo":0.3,"qr_code":"qr_valore","quantita":3,"key":"x4idec"},{"nome":"insalata di mare","prezzo":0.3,"qr_code":"qr_valore","quantita":3,"key":"d2jdss"}]