admin管理员组

文章数量:1435859

I have a json object :

myJson = [
{"id":"001", "name":"AAA"},
{"id":"002", "name":"BBB"},
{"id":"003", "name":"CCC"},
{"id":"004", "name":"DDD"}
]

How I can remove an element by value of id?

thank for your helps

I have a json object :

myJson = [
{"id":"001", "name":"AAA"},
{"id":"002", "name":"BBB"},
{"id":"003", "name":"CCC"},
{"id":"004", "name":"DDD"}
]

How I can remove an element by value of id?

thank for your helps

Share Improve this question edited Nov 15, 2012 at 9:41 Bakudan 19.5k9 gold badges55 silver badges75 bronze badges asked Nov 15, 2012 at 9:33 ValerianeValeriane 9543 gold badges17 silver badges39 bronze badges 2
  • 4 You don't have a JSON object, you have an object. More specifically, you have an array of objects. The answer that silly posted is the simplest solution to removing an item, or you can use a standard loop to go through the array until you find the matching item and then use .splice() to remove it. – nnnnnn Commented Nov 15, 2012 at 9:36
  • 1 right. But I parsed this object to JSON – Valeriane Commented Nov 15, 2012 at 11:17
Add a ment  | 

1 Answer 1

Reset to default 5

you can filter your array... as example: you want to remove every object with the id "003" use this:

myJson = myJson.filter(function(jsonObject) {
    return jsonObject.id != "003";
});

本文标签: javascriptjson remove element by valueStack Overflow