我在Google和Google上搜索了很多關於如何從我的JSON對象中刪除元素的東西。Javascript如何添加和刪除2級深度的JSON元素
這是我簡化的JSON來說明我的要求。
包:
{
"resourceType": "Bundle",
"meta": {
"lastUpdated": "2017-10-06T04:42:22.411Z"
},
"type": "searchset",
"total": "0",
"entry": [
{
"_id": "59d5739e668e9e3fd29aeb0d",
"resource": {
"id": "59d5739e668e9e3fd29aeb0d"
},
"__v": 0
},
{
"_id": "59d6a3fae4b45d50c5ffd4f7",
"resource": {
"id": "59d6a3fae4b45d50c5ffd4f7"
},
"__v": 0
},
{
"_id": "59d6a831e4b45d50c5ffd4fa",
"resource": {
"id": "59d6a831e4b45d50c5ffd4fa"
},
"__v": 0
}
]
}
如何刪除所有entry._id和入門.__ V' 我試過這些,但沒有工作。
delete bundle.meta; <-- meta is on level 1, it works.
delete bundle.entry._id; <-- not working. The _id under entry
delete bundle.__v; <-- not working. The __v is also under entry
我如何在每個條目下添加了新的元素,如「全名」,如下
我想要的結果 - 刪除「_id」和「__v」,然後添加「全名」的條目[ ]:
{
"resourceType": "Bundle",
"meta": {
"lastUpdated": "2017-10-06T04:42:22.411Z"
},
"type": "searchset",
"total": "0",
"entry": [
{
"fullname": "Apple",
"resource": {
"id": "59d5739e668e9e3fd29aeb0d",
}
},
{
"fullname": "Orange",
"resource": {
"id": "59d6a3fae4b45d50c5ffd4f7"
},
}, ......
我已經嘗試了很多方法並搜索了很多。謝謝您的幫助!
感謝Vivek的回答。有用。
for (var i = 0; i < bundle.entry.length; i++) {
delete bundle.entry[i]._id;
delete bundle.entry[i].__v;
bundle.entry[i].fullname = "Test";
}
_「我希望的結果 - 刪除 '_id' 和 '__v',然後添加「FUL lname'到條目[]「_」全名「屬性值如何確定? – guest271314
您無法從[JSON對象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON)中移除元素... – Teemu
嗨guest271314,讓我們來說說條目[i] .fullname =條目[i] .resource.id的最後4位;謝謝您的幫助。 – Autorun