5

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

4
  • 1
    You're using not valid data structure Commented Mar 21, 2016 at 9:41
  • your data is not valid. either an array or an object, but the property is missing Commented Mar 21, 2016 at 9:41
  • ok can you please explain by saying data is not valid..... 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! Commented Mar 21, 2016 at 9:43

7 Answers 7

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

Sign up to request clarification or add additional context in comments.

8 Comments

You can directly do return emp.id != id. Look for @Jamiec's answer
@Rajesh yeah, I see, thanks. Maybe my code more explains the logic of filter method, without magic
@isvforall if I want to select a particular node based on the id , like let say id = 3; it will give me a object {id: "3", name: "X-Men", type: "action"}, and I can do some business operation on it so how can I find this.
@ankur so how can I find this find what? an index of that object in an array?
@ankur - thats the exact opposite of what your question asked!
|
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!

Comments

1

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

Comments

0

try

using jquery you can do like this

function RemoveNode(id)
{

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

2 Comments

There was absolutely no need for JQuery in this answer! All that burdon for $.each!
Why do you need to use jQuery? Can't you do the same thing with data.forEach?
0

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

Comments

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

1 Comment

If there are more than one matching elements, this will only remove one of them.
0

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

1 Comment

I guess this is not what OP is looking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.