我想从JSON中删除JSON元素或整行。
我有以下JSON字符串:
{
"result":[
{
"FirstName": "Test1",
"LastName": "User",
},
{
"FirstName": "user",
"LastName": "user",
},
{
"FirstName": "Ropbert",
"LastName": "Jones",
},
{
"FirstName": "hitesh",
"LastName": "prajapti",
}
]
}
答案 0 :(得分:145)
var json = { ... };
var key = "foo";
delete json[key]; // Removes json.foo from the dictionary.
您可以使用splice从数组中删除元素。
答案 1 :(得分:19)
JSON中没有逗号逗号
更新:如果要从对象中的数组中删除项目,则需要使用array.splice而不删除
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.
答案 2 :(得分:11)
您可以尝试删除JSON,如下所示:
var bleh = {first: '1', second: '2', third:'3'}
alert(bleh.first);
delete bleh.first;
alert(bleh.first);
或者,您也可以传入索引以删除属性:
delete bleh[1];
但是,要了解使用删除的一些后果,请查看here
答案 3 :(得分:6)
答案 4 :(得分:5)
所有的答案都很棒,它也会按照你的要求做,但我相信删除它的最佳方法,以及垃圾收集器的最佳方法(如果你运行node.js)是这样的:
var json = { <your_imported_json_here> };
var key = "somekey";
json[key] = null;
delete json[key];
这样node.js
的垃圾收集器就会知道不再需要json['somekey']
,并将其删除。
答案 5 :(得分:5)
试试以下
var myJSONObject ={"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
console.log(myJSONObject);
console.log(myJSONObject.ircEvent);
delete myJSONObject.ircEvent
delete myJSONObject.regex
console.log(myJSONObject);
答案 6 :(得分:4)
如@mplungjan所述,我虽然是对的。然后我马上点击up rate按钮。但通过遵循它,我终于得到了一个错误。
<script>
var data = {"result":[
{"FirstName":"Test1","LastName":"User","Email":"test@test.com","City":"ahmedabad","State":"sk","Country":"canada","Status":"False","iUserID":"23"},
{"FirstName":"user","LastName":"user","Email":"u@u.com","City":"ahmedabad","State":"Gujarat","Country":"India","Status":"True","iUserID":"41"},
{"FirstName":"Ropbert","LastName":"Jones","Email":"Robert@gmail.com","City":"NewYork","State":"gfg","Country":"fgdfgdfg","Status":"True","iUserID":"48"},
{"FirstName":"hitesh","LastName":"prajapti","Email":"h.prajapati@zzz.com","City":"","State":"","Country":"","Status":"True","iUserID":"78"}
]
}
alert(data.result)
delete data.result[3]
alert(data.result)
</script>
删除只是删除数据,但“地点”仍然存在 undefined 。
我这样做了,它就像一个魅力:
data.result.splice(2,1);
含义:删除位置3处的1个项目(因为数组从0开始计算,然后第3个项目计为第2个)
答案 7 :(得分:1)
我建议使用<allow-intent href="*" />
<allow-navigation href="*" />
方法从JSON对象数组中删除对象。
splice
我使用这个是因为当我使用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.
}
});
方法时,我会delete
null
个对象
答案 8 :(得分:1)
如果我们要从数组中删除一个属性,请说“ firstName” 我们可以将map函数与如上所述的delete一起使用
var result= [
{
"FirstName": "Test1",
"LastName": "User",
},
{
"FirstName": "user",
"LastName": "user",
},
{
"FirstName": "Ropbert",
"LastName": "Jones",
},
{
"FirstName": "hitesh",
"LastName": "prajapti",
}
]
result.map( el=>{
delete el["FirstName"]
})
console.log("OUT",result)
答案 9 :(得分:1)
对于那些来这里寻求如何根据对象值从JSON数组中删除对象的人
let users = [{"name": "Ben"},{"name": "Tim"},{"name": "Harry"}];
for (let [i, user] of users.entries()) {
if (user.name == "Tim") {
users.splice(i, 1);
}
}
用户 Tim 现在已从JSON数组用户中删除。
答案 10 :(得分:0)
尝试
json = $.grep(newcurrPayment.paymentTypeInsert, function (el, idx) { return el.FirstName == "Test1" }, true)