最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - json remove element by value - Stack Overflow

matteradmin6PV0评论

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";
});
Post a comment

comment list (0)

  1. No comments so far