2017-10-06 89 views
0

我在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"; 
} 
+0

_「我希望的結果 - 刪除 '_id' 和 '__v',然後添加「FUL lname'到條目[]「_」全名「屬性值如何確定? – guest271314

+0

您無法從[JSON對象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON)中移除元素... – Teemu

+0

嗨guest271314,讓我們來說說條目[i] .fullname =條目[i] .resource.id的最後4位;謝謝您的幫助。 – Autorun

回答

1

請嘗試以下代碼,測試,它可以幫助你。

var object1 = { 
    "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 
     } 
    ] 
    }; 

    var array1=['Apple','Orange','Gava'] 

    function a(){ 
     for(var i=0;i<object1.entry.length;i++){ 
      delete object1.entry[i]._id; 
      delete object1.entry[i].__v; 
      object1.entry[i].fullname = array1[i]; 
     } 
     console.log(object); 
    } 

如果您覺得有幫助,標記會有幫助。

+0

感謝您的簡單答案。有用。 – Autorun

1

bundle.entry是一個數組。 :)
刪除第一個元素的_id:
delete bundle.entry[0]._id
刪除_id和__v所有元素:
bundle.entry = bundle.entry.map(function(entry){ var newEntry = { resource: entry.resource, fullname: "fullname here" }; return newEntry; });

+0

這將在每個條目中添加相同的「全名」。不想要的 – Prasanna

1
const fullNameList = ['Apple', 'Orange', 'Guava'] 
delete bundle.meta 
bundle.entry = bundle.entry.map((item, index) => { 
    const {__v, _id, ...restObj } = item; 
    restObj.fullname = fullNameList[index] 
    return restObj; 
}) 

記住,fullNameList的長度和bundle.entry的長度必須相同

1

您可以使用地圖功能和從對象中刪除鍵,而在同一個函數添加鍵,如下圖所示:

var data = { 
 
    "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 
 
     } 
 
    ] 
 
} 
 
var names = ['A','B','C']; 
 
var counter = 0; 
 
data.entry = data.entry.map(function(obj){ 
 
    delete obj['_id'] 
 
    delete obj['__v'] 
 
    obj.fullname = names[counter] 
 
    counter++ ; 
 
    return obj 
 
}); 
 

 
console.log(data);

+0

感謝約瑟夫這個想法。 – Autorun