Reputation: 4733
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
Upvotes: 5
Views: 14122
Reputation: 91
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);
Upvotes: 0
Reputation: 8926
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));
Upvotes: 6
Reputation: 23816
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);
Upvotes: 1
Reputation: 386654
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>');
Upvotes: 0
Reputation: 24925
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>");
Upvotes: 0
Reputation: 31
try
using jquery you can do like this
function RemoveNode(id)
{
$.each(data,function (key,val) {
if(val.Id == id)
{
delete data[key];
}
}
}
Upvotes: 0
Reputation: 136114
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
!
Upvotes: 4