我有array
个嵌套的object
如何删除每个以下划线开头的key/value
对?
示例:{ id: 1, _link: true } => { id, 1 }
这是一个更完整的数据示例:
{
"data": [{
"name": "te4ste",
"projectId": 1,
"productAndServicesCategoryList": [{
"name": "b",
"productAndServicesId": 1,
"productAndServicesItemList": [{
"name": "w",
"productAndServicesCategoryId": 2,
"description": "w",
"version": null,
"createdDate": "2019-04-09T23:17:46.857Z",
"createdById": null,
"lastModifiedById": null,
"deleted": false,
"_links": {
"self": {
"href": "projects/services/contact-emails/emails/2"
}
},
"id": 2
}],
"description": "b",
"version": null,
"createdDate": "2019-04-09T22:24:37.508Z",
"createdById": null,
"lastModifiedById": null,
"deleted": false,
"_links": {
"self": {
"href": "projects/services/contact-emails/groups/2"
}
},
"id": 2
},
{
"name": "a",
"productAndServicesId": 1,
"productAndServicesItemList": [{
"name": "c",
"productAndServicesCategoryId": 1,
"description": "c",
"version": null,
"createdDate": "2019-04-09T22:24:46.332Z",
"createdById": null,
"lastModifiedById": null,
"deleted": false,
"_links": {
"self": {
"href": "projects/services/contact-emails/emails/1"
}
},
"id": 1
}],
"description": "a",
"version": null,
"createdDate": "2019-04-09T22:24:32.717Z",
"createdById": null,
"lastModifiedById": null,
"deleted": false,
"_links": {
"self": {
"href": "projects/services/contact-emails/groups/1"
}
},
"id": 1
}
],
"description": "testset",
"version": null,
"createdDate": "2019-04-09T22:24:22.563Z",
"createdById": null,
"lastModifiedById": null,
"deleted": false,
"_links": {
"self": {
"href": "projects/services/newsletters/groups/1"
}
},
"id": 1
}],
"total": 1
}
答案 0 :(得分:1)
您可以创建一个递归函数,以查看传入的对象的类型并采取相应的措施。对于数组,将每个项目传递回对象,删除键并传递子代。这将改变对象的位置:
let data= [{"name": "te4ste","projectId": 1,"productAndServicesCategoryList": [{"name": "b","productAndServicesId": 1,"productAndServicesItemList": [{"name": "w","productAndServicesCategoryId": 2,"description": "w","version": null,"createdDate": "2019-04-09T23:17:46.857Z","createdById": null,"lastModifiedById": null,"deleted": false,"_links": {"self": {"href": "projects/services/contact-emails/emails/2"}},"id": 2}],"description": "b","version": null,"createdDate": "2019-04-09T22:24:37.508Z","createdById": null,"lastModifiedById": null,"deleted": false,"_links": {"self": {"href": "projects/services/contact-emails/groups/2"}},"id": 2},{"name": "a","productAndServicesId": 1,"productAndServicesItemList": [{"name": "c","productAndServicesCategoryId": 1,"description": "c","version": null,"createdDate": "2019-04-09T22:24:46.332Z","createdById": null,"lastModifiedById": null,"deleted": false,"_links": {"self": {"href": "projects/services/contact-emails/emails/1"}},"id": 1}],"description": "a","version": null,"createdDate": "2019-04-09T22:24:32.717Z","createdById": null,"lastModifiedById": null,"deleted": false,"_links": {"self": {"href": "projects/services/contact-emails/groups/1"}},"id": 1}],"description": "testset","version": null,"createdDate": "2019-04-09T22:24:22.563Z","createdById": null,"lastModifiedById": null,"deleted": false,"_links": {"self": {"href": "projects/services/newsletters/groups/1"}},"id": 1}]
function remove_s(obj){
if (!obj || typeof obj !== 'object') return
Object.keys(obj).forEach(k => {
if (k.startsWith('_')) delete obj[k]
else remove_s(obj[k])
})
}
remove_s(data)
console.log(data)
答案 1 :(得分:-1)
confidence == TRUE