Error when deleting some properties of array objects typescript/JavaScript

jimbo R

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?

Plunker

Estus Flask

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.

edited at
0

Comments

0 comments
Login to comment

Related

Core Data: Error when deleting/adding objects

Remove some properties from array of javascript objects

compare objects of same array and group their some properties when specific property matched with other object

Error when deleting dynamic array c++

How to merge some properties in array of objects into another array?

Deleting properties from nested objects

Deleting an array of Class Objects

Getting error while deleting the key value from array of objects

AWS S3 - UserKeyMustBeSpecified error when deleting multiple objects

Merge duplicates in array of objects and mutate some properties javascript

Filter array of objects by values when not all objects have some keys

Adding an Array of Objects with some new properties to another Array of Objects which has same name

Array of properties of objects in an array

Deleting an element from an array of objects

TypeScript keyed array of JSON objects missing properties error

TypeScript: Type error when sorting an array of objects

Syntax error when console logging an array of objects

Error when creating array of objects in a javascript loop

An error comes when mapping array of objects in React

Array of objects to objects with properties as keys

How to access object's properties when looping through an array of objects?

Sum properties array of Objects

Required properties on array of objects

Random Array of Objects & Properties

Access properties of an array of objects

Mismatching properties in array of objects

How can I create an array of objects based on some of another array object's properties?

JavaScript: Error when destructuring an array of objects when the array is empty

Error when deleting microposts. For some reason the GET method is requested for https