从JSON数组中搜索并删除对象

5

我将尝试搜索一个对象并从JSON数组中删除它。

我的JSON对象数组如下:

var data = [{
    {id: "1", name: "Snatch", type: "crime"},
    {id: "2", name: "Witches of Eastwick", type: "comedy"},
    {id: "3", name: "X-Men", type: "action"},
    {id: "4", name: "Ordinary People", type: "drama"},
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
}];

我希望实现的是,如果我有一个id为1的对象,我可以搜索数组并将其与数组匹配,然后从数组中删除它。
我尝试使用以下代码来实现:
function RemoveNode(id)
{
 data.forEach(function (emp) {
   if(emp.Id == id)
    {
      delete emp;
    }
  }
}

我无法让它正常工作,请建议更好的方法来解决这个问题。


1
您正在使用无效的数据结构。 - isvforall
您的数据无效。它应该是一个数组或对象,但缺少属性。 - Nina Scholz
好的,你能否解释一下“数据无效”的含义? - ankur
1
@ankur data 应该是一个数组 (var data = [...]) 或者是一个带有属性的对象 (var data = {arr:[..]})。目前它是一个没有属性名的对象! - Jamiec
7个回答

6
你正在使用无效的数据结构,你的数组需要用方括号[]括起来。
对于你的情况,最好使用filter函数:

var data = [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "2", name: "Witches of Eastwick", type: "comedy"},
    {id: "3", name: "X-Men", type: "action"},
    {id: "4", name: "Ordinary People", type: "drama"},
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
];

function RemoveNode(id) {
    return data.filter(function(emp) {
        if (emp.id == id) {
            return false;
        }
        return true;
    });
}

var newData = RemoveNode("1");

document.write(JSON.stringify(newData, 0, 4));


1
你可以直接使用 return emp.id != id。请查看 @Jamiec 的回答。 - Rajesh
@isvforall 如果我想根据id选择特定的节点,比如说id = 3; 它会给我一个对象 {id: "3", name: "X-Men", type: "action"},然后我可以对它进行一些业务操作,那么我该如何找到它呢? - ankur
@ankur 那你要找什么 找什么?在数组中找到该对象的索引吗? - isvforall
@isvforall 希望将对象存储在变量中,例如 var single = //执行某些操作以获取单个对象。因此,如果我执行 single.id,它会给我值 3,而 single.name 将给我 x-men。 - ankur
1
@ankur - 这恰恰相反于你的问题所询问的! - Jamiec
显示剩余3条评论

4
更好的方法可能是使用filter函数来过滤掉原始数组中不需要的元素。
假设data确实是一个对象数组(即当前问题中没有错误)。
function RemoveNode(id){
    return data.filter(function(e){
        return e.id !== id;
    });
}

你也可以使用splice,但这需要知道你想要删除的项目的索引,而当你找到它时,你可能会选择直接filter

1
考虑以下代码,其中使用了forEachsplice函数,假设data是一个对象数组:
var data = [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "2", name: "Witches of Eastwick", type: "comedy"},
    {id: "3", name: "X-Men", type: "action"},
    {id: "4", name: "Ordinary People", type: "drama"},
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
];

function RemoveNode(id){
    data.forEach(function(e, index){
    if(id == e.id){
        data.splice(index, 1);
    }
  })
}
RemoveNode(1);
console.log(data);

0

尝试一下

使用jQuery,您可以这样做

function RemoveNode(id)
{

 $.each(data,function (key,val) {
   if(val.Id == id)
    {
      delete data[key];
    }
  }
}

这个答案完全不需要 JQuery!所有的负担都落在 $.each 上了! - Jamiec
为什么需要使用jQuery?你不能用data.forEach做同样的事情吗? - Barmar

0

你必须将数组分配给对象/JSON中的一个键。

然后使用filter在每个对象上设置条件! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

代码:

var data = {"data": [
    {"id": 1, "name": "New York"},
    {"id": 2, "name": "Dubai"},
    {"id": 3, "name": "Brabrand"},
    {"id": 4, "name": "Anything"}
]}.data;

var removeId = 2

var newData = data.filter(function(object) {
  return object.id !== removeId;
}) 

console.log(newData);

我猜这不是 OP 所寻找的。 - Rajesh

0

你需要使用从数组中删除而不是当前迭代的方法。

你可以尝试这个:

var data = [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "2", name: "Witches of Eastwick", type: "comedy"},
    {id: "3", name: "X-Men", type: "action"},
    {id: "4", name: "Ordinary People", type: "drama"},
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
];

data.forEach(function(emp, index){
  if(emp.id==1){
    delete data[index];
  }
});

document.write("<pre>" + JSON.stringify(data,0,4) + "</pre>")

如果您想根据条件删除值,可以尝试使用Array.filter

var data = [
  {id: "1", name: "Snatch", type: "crime"},
  {id: "2", name: "Witches of Eastwick", type: "comedy"},
  {id: "3", name: "X-Men", type: "action"},
  {id: "4", name: "Ordinary People", type: "drama"},
  {id: "5", name: "Billy Elliot", type: "drama"},
  {id: "6", name: "Toy Story", type: "children"}
];

var result = data.filter(function(emp){ return emp.id != 1 });

document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");


0

I suggest to use Array#some() in combination with Array#splice()

var data = [{ id: "1", name: "Snatch", type: "crime" }, { id: "2", name: "Witches of Eastwick", type: "comedy" }, { id: "3", name: "X-Men", type: "action" }, { id: "4", name: "Ordinary People", type: "drama" }, { id: "5", name: "Billy Elliot", type: "drama" }, { id: "6", name: "Toy Story", type: "children" }];

function del(id) {
    var index;
    data.some(function (a, i) {
        if (a.id === id) {
            index = i;
            return true;
        }
    }) && data.splice(index, 1);
}

del('1');
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');


如果有多个匹配元素,这将只删除其中一个。 - Barmar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接