I have an array of objects (firstList), now I am creating a new array (exportData) by clone the array firstList, with this new array I want to delete some of its properties and I do the following:
let exportData = this.firstList;
exportData = exportData.filter(function (props) {
delete props.job;
return true;
});
Here I do not understand why even my old arrays are also deleting those properties, due to the mechanism or what did I do wrong?
filter
is misused here because it doesn't actually filter anything, just iterates over array elements. For array iteration generic loop (for
, for..of
, forEach
) should be used.
The problem here is that props
object is not cloned. When it's modified, changes appear in every place where it's used.
The array should be mapped to shallow copies of objects where job
property is omitted. This can be conventionally done with spread and rest syntax:
exportData = exportData.map(({ job, ...props}) => ({...props}));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments