我有這種型別的 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"
}]
uj5u.com熱心網友回復:
通過使用 ES6 擴展運算子,您可以實作:無論您想洗掉什么,在引數中給出該鍵,然后...rest回傳其余引數。
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);
uj5u.com熱心網友回復:
你可以通過解構你的物件來做到這一點。
[{ 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)
uj5u.com熱心網友回復:
您可以使用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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/426649.html
上一篇:為變數名Python傳遞字串
