I have a function as below which finds a particular element in an array of objects and updates it:
async update(id, attrs){
const records = await this.getAll();
const record = records.find(record => record.id === id);
Object.assign(record, attrs);
await this.writeAll(records);
}
I was surprised to understand the find method returns a reference to the matching element in the records array whereas I was expecting that the record variable would just contain the value and changing it would not cause the original records array to get updated.
So my question is, what if I just want to retrieve the value from an object array but not a reference to the actual element? that is, I need to be able to update the returned value without mutating the original array.