从json数组

时间:2016-03-21 09:38:23

标签: javascript jquery arrays json

我正在尝试搜索对象并从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;
    }
  }
}

我无法让它发挥作用,请提出更好的方法来做到这一点

7 个答案:

答案 0 :(得分:4)

更好的方法可能是filter您的原始数组,以删除您不想要的项目。

假设data实际上是一个对象数组(即,你的问题中目前没有错误)

function RemoveNode(id){
    return data.filter(function(e){
        return e.id !== id;
    });
}

您也可以使用splice,但这需要知道您要删除的项目的索引,并且当您发现自己可能已经删除时只是filter

答案 1 :(得分:3)

您使用的数据结构不正确,您的数组需要放在方括号typings

为了您的情况,最好使用[]功能:

filter

答案 2 :(得分: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>");

答案 3 :(得分:0)

我建议将Array#some()Array#splice()结合使用

&#13;
&#13;
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>');
&#13;
&#13;
&#13;

答案 4 :(得分:0)

请考虑以下代码forEach&amp; splice,假设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);

答案 5 :(得分:0)

您必须将数组分配给对象/ json中的键。

然后使用过滤器为每个对象制作一个条件! 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);

答案 6 :(得分:-1)

使用jquery你可以这样做

function RemoveNode(id)
{

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