Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove JSON entry by value [duplicate]

Possible Duplicate:
Delete from array in javascript

I have the following JSON object:

[id:84,id:92,id:123,id:2353]

How would I go about removing the item which the value is "123" using javascript?

or if I formatted the json as

[84, 92, 123, 2353]

How would it be removed in this case?

like image 328
André Figueira Avatar asked Jan 18 '13 14:01

André Figueira


2 Answers

Assume you have this:

var items  = [{ id: 84 }, { id: 92 }, { id: 123 }, { id: 2353 }];

var filtered = items.filter(function(item) { 
   return item.id !== 123;  
});

//filtered => [{ id: 84 }, { id: 92 }, { id: 2353 }]
like image 109
nekman Avatar answered Nov 06 '22 01:11

nekman


Supposing you actually have an object from a json in the json variable

for (key in json) {
    if (json.hasOwnProperty(key) && json[key] == 123) {
        delete json[key];
    }
}
like image 25
Magus Avatar answered Nov 06 '22 00:11

Magus