I have a function that receives an array of items as a parameter. Within this function, I need to locate a specific item and update one of its properties.
const defaultGroup = find(groupedCustomFields, group => group.name === DEFAULT_GROUP); //[find][1] - it's `lodash` function
defaultGroup.fields = defaultGroup.fields.filter(f => !f.isGroupEditable);
This code selects the first matching item from the collection and modifies an object property.
The issue arises when my input collection is also being altered since the found item gets updated within it. To prevent this, I believe I should work with a copy of the item instead.
What would be the most effective approach in achieving this?
const defaultGroup = find(groupedCustomFields, group => group.name === DEFAULT_GROUP),
copyOfDefGroup = Object.assign({}, defaultGroup);
Would using this code be recommended for handling this scenario?