Javascript 移除 json 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5310304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove json element
提问by Hitesh Prajapati
I want to remove JSON element or one whole row from JSON.
我想从 JSON 中删除 JSON 元素或一整行。
I have following JSON string:
我有以下 JSON 字符串:
{
"result":[
{
"FirstName": "Test1",
"LastName": "User",
},
{
"FirstName": "user",
"LastName": "user",
},
{
"FirstName": "Ropbert",
"LastName": "Jones",
},
{
"FirstName": "hitesh",
"LastName": "prajapti",
}
]
}
回答by dteoh
回答by mplungjan
Do NOT have trailing commas in your JSON
JSON 中没有尾随逗号
UPDATE: you need to use array.spliceand not delete if you want to remove items from the array in the object
更新:如果要从对象中的数组中删除项目,则需要使用array.splice而不是 delete
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.
回答by Farax
You can try to delete the JSON as follows:
您可以尝试按如下方式删除 JSON:
var bleh = {first: '1', second: '2', third:'3'}
alert(bleh.first);
delete bleh.first;
alert(bleh.first);
Alternatively, you can also pass in the index to delete an attribute:
或者,您也可以传入索引来删除属性:
delete bleh[1];
However, to understand some of the repercussions of using deletes, have a look here
但是,要了解使用删除的一些影响,请查看此处
回答by Sahan
I recommend splice
method to remove an object from JSON objects array.
我推荐splice
从 JSON 对象数组中删除对象的方法。
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.
}
});
I use this because when I use delete
method, I get null
object after I do JSON.stringify(json)
我使用这个是因为当我使用delete
方法时,我会null
在我做之后得到对象JSON.stringify(json)
回答by Quentin
- Fix the errors in the JSON: http://jsonlint.com/
- Parse the JSON (since you have tagged the question with JavaScript, use json2.js)
- Deletethe property from the object you created
- Stringify the object back to JSON.
- 修复 JSON 中的错误:http: //jsonlint.com/
- 解析 JSON(因为你已经用 JavaScript 标记了问题,所以使用json2.js)
- 从您创建的对象中删除属性
- 将对象字符串化回 JSON。
回答by Games Brainiac
All the answers are great, and it will do what you ask it too, but I believe the best way to delete this, and the best way for the garbage collector (if you are running node.js) is like this:
所有的答案都很棒,它也会按照您的要求执行,但我相信删除它的最佳方法以及垃圾收集器的最佳方法(如果您正在运行 node.js)是这样的:
var json = { <your_imported_json_here> };
var key = "somekey";
json[key] = null;
delete json[key];
This way the garbage collector for node.js
will know that json['somekey']
is no longer required, and will delete it.
这样垃圾收集器node.js
就会知道json['somekey']
不再需要它,并将其删除。
回答by abcd
Try this following
试试下面这个
var myJSONObject ={"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
console.log(myJSONObject);
console.log(myJSONObject.ircEvent);
delete myJSONObject.ircEvent
delete myJSONObject.regex
console.log(myJSONObject);
回答by Sulung Nugroho
As described by @mplungjan, I though it was right. Then right away I click the up rate button. But by following it, I finally got an error.
正如@mplungjan 所描述的,我认为这是对的。然后我马上点击了加价按钮。但是通过遵循它,我终于得到了一个错误。
<script>
var data = {"result":[
{"FirstName":"Test1","LastName":"User","Email":"[email protected]","City":"ahmedabad","State":"sk","Country":"canada","Status":"False","iUserID":"23"},
{"FirstName":"user","LastName":"user","Email":"[email protected]","City":"ahmedabad","State":"Gujarat","Country":"India","Status":"True","iUserID":"41"},
{"FirstName":"Ropbert","LastName":"Jones","Email":"[email protected]","City":"NewYork","State":"gfg","Country":"fgdfgdfg","Status":"True","iUserID":"48"},
{"FirstName":"hitesh","LastName":"prajapti","Email":"[email protected]","City":"","State":"","Country":"","Status":"True","iUserID":"78"}
]
}
alert(data.result)
delete data.result[3]
alert(data.result)
</script>
Delete is just remove the data, but the 'place' is still there as undefined.
Delete 只是删除数据,但 'place' 仍然作为undefined 存在。
I did this and it works like a charm :
我这样做了,它就像一个魅力:
data.result.splice(2,1);
meaning : delete 1 item at position 3 ( because array is counted form 0, then item at no 3 is counted as no 2 )
含义:删除位置 3 处的 1 个项目(因为数组从 0 开始计数,那么第 3 个项目被视为没有 2)
回答by Kamran Ul Haq
try this
尝试这个
json = $.grep(newcurrPayment.paymentTypeInsert, function (el, idx) { return el.FirstName == "Test1" }, true)