Home > Software engineering >  Remove Object from Array of Object in Angular
Remove Object from Array of Object in Angular

Time:02-17

I have this type of JSON and what I want, to remove the complete object of StartGeotag from array of object.

[{ CreatedDate: "2022-02-17T10:30:07.0442288Z" DeletedDate: null ProjectId: "05b76d03-8c4b-47f4-7c20-08d9e2990812"
StartGeotag: {Type: 'Point', Latitude: '33.6607231', CreatedDate: '2022-02- :34:46.5389961Z'}
StartTime: "2022-02-17T10:30:05.828Z"}]

CodePudding user response:

You can do it by destructuring your object.

[{ CreatedDate: "2022-02-17T10:30:07.0442288Z" , DeletedDate: null, ProjectId: "05b76d03-8c4b-47f4-7c20-08d9e2990812",
    StartGeotag: {Type: 'Point', Latitude: '33.6607231', CreatedDate: '2022-02- :34:46.5389961Z'},
    StartTime: "2022-02-17T10:30:05.828Z"}].map(({StartGeotag, ...item}) => item)

If you want to take only the StartGeoTag object than you can do it in this way:

 [{ CreatedDate: "2022-02-17T10:30:07.0442288Z" , DeletedDate: null, ProjectId: "05b76d03-8c4b-47f4-7c20-08d9e2990812",
    StartGeotag: {Type: 'Point', Latitude: '33.6607231', CreatedDate: '2022-02- :34:46.5389961Z'},
    StartTime: "2022-02-17T10:30:05.828Z"}].map(({StartGeotag}) => StartGeotag)

CodePudding user response:

By using ES6 Spread Operator you can achieve this: Whatever you want to remove, give that key in argument, and ...rest will return the rest arguments.

var data = [{ CreatedDate: "2022-02-17T10:30:07.0442288Z", 
DeletedDate: null, 
ProjectId: "05b76d03-8c4b-47f4-7c20-08d9e2990812",
StartGeotag: {Type: 'Point', Latitude: '33.6607231', 
CreatedDate: '2022-02- :34:46.5389961Z'},
StartTime: "2022-02-17T10:30:05.828Z"}]

const res= data.map(({StartGeotag, ...rest}) => ({...rest})); 

console.log(res);

  • Related