Remove json element
我想从JSON中删除JSON元素或整行。
我有以下JSON字符串:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | { "result":[ { "FirstName":"Test1", "LastName": "User", }, { "FirstName":"user", "LastName": "user", }, { "FirstName":"Ropbert", "LastName": "Jones", }, { "FirstName":"hitesh", "LastName": "prajapti", } ] } |
1 2 3 | var json = { ... }; var key ="foo"; delete json[key]; // Removes json.foo from the dictionary. |
你可以使用剪接to remove元素从一阵列。 </P >
不要在你的有尾commas JSON </P >
更新:你要用array.splice与注释删除了如果你想去解除项目从数组的对象 </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | var data = { "result": [{ "FirstName":"Test1", "LastName":"User" }, { "FirstName":"user", "LastName":"user" }] } console.log(data.result); console.log("------------ deleting -------------"); delete data.result[1]; console.log(data.result); // note the"undefined" in the array. data = { "result": [{ "FirstName":"Test1", "LastName":"User" }, { "FirstName":"user", "LastName":"user" }] } console.log(data.result); console.log("------------ slicing -------------"); var deletedItem = data.result.splice(1,1); console.log(data.result); // here no problem with undefined. |
</P >
你可以尝试到删除的JSON养的是: </P >
1 2 3 4 5 6 7 | var bleh = {first: '1', second: '2', third:'3'} alert(bleh.first); delete bleh.first; alert(bleh.first); |
alternatively也护照,你可以删除的索引的属性:安 </P >
1 | delete bleh[1]; |
然而,对理解some of the repercussions利用deletes,这里有一个面貌 </P >
这下面的尝试 </P >
1 2 3 4 5 6 | var myJSONObject ={"ircEvent":"PRIVMSG","method":"newURI","regex":"^http://.*"}; console.log(myJSONObject); console.log(myJSONObject.ircEvent); delete myJSONObject.ircEvent delete myJSONObject.regex console.log(myJSONObject); |
所有的答案都是伟大的,和它会做你的盒子,它太,但相信最好的方式到删除这是最好的方式,和"垃圾集(如果你是跑步的Node.js冰呢:) </P >
1 2 3 4 | var json = { <your_imported_json_here> }; var key ="somekey"; json[key] = null; delete json[key]; |
这条路是用垃圾集
为描述mplungjan村",虽然它是正确的。然后单击鼠标右键,在弹出的"远离速率的按钮。但村以下的信息,我有个错误。 </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 | var data = {"result":[ {"FirstName":"Test1","LastName":"User","Email":"[email protected]","City":"ahmedabad","State":"sk","Country":"canada","Status":"False","iUserID":"23 <hr> <p> I recommend <wyn>splice</wyn> method to remove an object from JSON objects array. </p> [cc lang="javascript"]jQuery(json).each(function (index){ if(json[index].FirstName =="Test1"){ json.splice(index,1); // This will remove the object that first name equals to Test1 return false; // This will stop the execution of jQuery each loop. } }); |
这是因为当在使用中的使用方法