从 Angular 中的对象数组中删除对象

Remove Object from Array of Object in Angular

我有这种类型的 JSON 和我想要的,从对象数组中删除 StartGeotag 的完整对象。

[{
    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"
}]

您可以通过解构对象来实现。

[{ 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)

如果你只想获取 StartGeoTag 对象,你可以这样做:

 [{ 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)

通过使用 ES6 Spread Operator,您可以实现这一点: 无论你想删除什么,在参数中给出那个键,...rest 将 return 其余参数。

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);

您可以使用对象 destructuring assignment from ES6.

工作演示:

let jsonObj = [{
    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"
}];

let res = jsonObj.map(({StartGeotag, ...remainingItems}) => remainingItems)

console.log(res);