删除 JSON 元素

我想从 JSON 中删除 JSON 元素或整行。

我有以下 JSON 字符串:

{
"result":[
{
"FirstName": "Test1",
"LastName":  "User",
},
{
"FirstName": "user",
"LastName":  "user",
},
{
"FirstName": "Ropbert",
"LastName":  "Jones",
},
{
"FirstName": "hitesh",
"LastName":  "prajapti",
}
]
}
576993 次浏览
  1. 修复 JSON: http://jsonlint.com/中的错误
  2. 解析 JSON (因为已经用 JavaScript 标记了问题,所以使用 Json2.js)
  3. 从您创建的对象中删除 属性
  4. 将对象字符串化为 JSON。

对象中不要有后面的逗号(JSON 是一个字符串表示法)

更新 : 如果要从对象中的数组中删除项,则需要使用 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.

var json = { ... };
var key = "foo";
delete json[key]; // Removes json.foo from the dictionary.

可以使用 拼接从数组中删除元素。

所有的答案都很棒,它也会按照你的要求去做,但是我相信最好的方法是删除它,对于垃圾收集器(如果你运行 node.js)最好的方法是这样的:

var json = { <your_imported_json_here> };
var key = "somekey";
json[key] = null;
delete json[key];

这样,node.js的垃圾收集器将知道不再需要 json['somekey'],并将其删除。

试试下面这个

var myJSONObject ={"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
console.log(myJSONObject);
console.log(myJSONObject.ircEvent);
delete myJSONObject.ircEvent
delete myJSONObject.regex
console.log(myJSONObject);

您可以尝试删除 JSON,如下所示:

var bleh = {first: '1', second: '2', third:'3'}


alert(bleh.first);


delete bleh.first;


alert(bleh.first);

或者,您也可以传入索引来删除一个属性:

delete bleh[1];

不过,要了解一些使用删除的反响,看看 给你

正如@mplungjan 所描述的,我认为这是正确的。然后我立刻点击提高价格按钮。但是通过跟踪,我最终得到了一个错误。

<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>

删除只是删除数据,但“地方”仍然存在作为 未定义

我这样做,它的工作原理就像一个符咒:

data.result.splice(2,1);

意思是: 在位置3处删除1项(因为数组从0开始计数,那么在位置3处的项就从2开始计数)

我建议使用 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.
}
});

我使用它是因为当我使用 delete方法时,我在执行 JSON.stringify(json)之后得到 null对象

试试这个

json = $.grep(newcurrPayment.paymentTypeInsert, function (el, idx) { return el.FirstName == "Test1" }, true)

如果我们想从数组中删除一个属性,请说“ 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)

对于那些来这里寻找 如何基于对象值从数组中移除对象:的人

let users = [{name: "Ben"},{name: "Tim"},{name: "Harry"}];


let usersWithoutTim = users.filter(user => user.name !== "Tim");


// The old fashioned way:


for (let [i, user] of users.entries()) {
if (user.name === "Tim") {
users.splice(i, 1); // Tim is now removed from "users"
}
}

注意: 这些函数将从数组中删除所有名为 Tim 的用户。

你可以使用过滤器吗? 假设你想删除罗伯特的 所有人实例

let result = [
{
"FirstName": "Test1",
"LastName":  "User",
},
{
"FirstName": "user",
"LastName":  "user",
},
{
"FirstName": "Ropbert",
"LastName":  "Jones",
},
{
"FirstName": "hitesh",
"LastName":  "prajapti",
}
]


result = result.filter(val => val.FirstName !== "Ropbert")


(result now contains)


[
{
"FirstName": "Test1",
"LastName":  "User",
},
{
"FirstName": "user",
"LastName":  "user",
},
{
"FirstName": "hitesh",
"LastName":  "prajapti",
}
]

您可以添加进一步的值,以缩小您删除的项目,例如,如果您想删除的第一个 还有姓,然后您可以这样做:

result = result.filter(val => !(val.FirstName === "Ropbert" && val.LastName === "Jones"))

尽管如上所述,如果在数组中有4个“ Robbert Jones”,那么 所有人4实例将被删除。

下降溶液在这里 我的 JSON 数组是

const [myData, setData] = useState([{username:'faisalamin', share:20}, {username:'john', share:80}])

我想在 renderItem 或.map 中单击按钮删除 john

const _deleteItem = (item) => {
const newData = myData.filter(value => { return value.username !== item.username });
setData(newData);
}