I have this react code, and I am selecting an item from the array with find()
method.
Does JS copy the item from the array or returns a reference?
EDIT:
My items in array are objects, like [{id: 12, name: "Peter"}, {id: 13, name: "Dorothy"}...]
this.setState(prevState => {
const items = [...prevState.items];
const itemToCopy = items.find(item => item.id === copiedItemId);
if(!itemToCopy) {
console.warn("Can not find an item to be copied!", copiedItemId, items, itemToCopy);
return null;
}
//is the variable "itemToCopy" a reference to item in the array or it is the new (copied) object itself?
itemToCopy.id = null;
itemToCopy.tempId = Math.random();
itemToCopy.actionBeingProcessed = "copy";
itemToCopy.date_raw = copyToDate;
//if I push it into the array, react tells me it has duplicate "tempId"
items.push(itemToCopy);
return {
items: sortItems(items)
};
}