最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Search and remove object from json array - Stack Overflow

programmeradmin2浏览0评论

I am trying to search a object and remove from json array

my json array of object looks like

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"}
}];

What I am try to achieve is if I have a object with Id=1
I can search the array match it with array and remove it from the array.

I am trying this by below code

function RemoveNode(id)
{
 data.forEach(function (emp) {
   if(emp.Id == id)
    {
      delete emp;
    }
  }
}

I am not able to get it work, kindly suggest a better way to do this

I am trying to search a object and remove from json array

my json array of object looks like

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"}
}];

What I am try to achieve is if I have a object with Id=1
I can search the array match it with array and remove it from the array.

I am trying this by below code

function RemoveNode(id)
{
 data.forEach(function (emp) {
   if(emp.Id == id)
    {
      delete emp;
    }
  }
}

I am not able to get it work, kindly suggest a better way to do this

Share Improve this question edited Mar 21, 2016 at 10:02 ankur asked Mar 21, 2016 at 9:38 ankurankur 4,73315 gold badges67 silver badges104 bronze badges 4
  • 1 You're using not valid data structure – isvforall Commented Mar 21, 2016 at 9:41
  • your data is not valid. either an array or an object, but the property is missing – Nina Scholz Commented Mar 21, 2016 at 9:41
  • ok can you please explain by saying data is not valid..... – ankur Commented Mar 21, 2016 at 9:42
  • 1 @ankur data should be either an array (var data = [...]) or an object with a property (var data = {arr:[..]}). Currently it is an object without a property name! – Jamiec Commented Mar 21, 2016 at 9:43
Add a comment  | 

7 Answers 7

Reset to default 6

You're using not valid data structure, your array needs to be in square brackets []

For your case better to use filter function:

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

A better way might be to filter your original array to remove the item you dont want.

Assuming data is really an array of objects (ie, does not have the error currently in your question)

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

You could also use splice, however that would require knowing the index of the item you want to remove, and by the time you've found that you may as well just filter!

Consider this following code with forEach & splice, assuming data is array of objects:

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

try

using jquery you can do like this

function RemoveNode(id)
{

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

You will have to use delete from array and not current iteration.

You can try this:

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

Also if you want to remove value based on condition, you should try 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>");

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>');

You must assign the array to a key in your object/json.

Then use filter to make a condition on every object! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Code:

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);
发布评论

评论列表(0)

  1. 暂无评论