-6

My javascript code like this :

<script type="text/javascript">
    var clubs = [ 
        {id: 1, name : 'chelsea'},
        {id: 2, name : 'city'},
        {id: 3, name : 'liverpool'}
    ];
    if(clubs.indexOf(2) != -1)
        clubs.splice(2, 1)
    console.log(clubs)
</script>

For example, I want to delete the index with id = 2

I try like that, but it does not work. The index with id = 2 is not deleted

How can I solve this problem?

Success Man
  • 5,513
  • 23
  • 109
  • 205
  • 4
    Possible duplicate of [How do I remove a particular element from an array in JavaScript?](https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript) – Aluan Haddad Apr 11 '18 at 04:58
  • Your array consists of objects but you're looking for the index of the _value_ `2`. Have a look [here](https://stackoverflow.com/questions/15287865/remove-array-element-based-on-object-property) for solutions. – H77 Apr 11 '18 at 05:33
  • Possible duplicate of [Javascript: Remove an element from an array of objects](https://stackoverflow.com/questions/18958429/javascript-remove-an-element-from-an-array-of-objects) – leaf Apr 11 '18 at 05:36

3 Answers3

1

Here is a way to solve your problem, check this :

var clubs = [
  { id: 1, name: 'chelsea' },
  { id: 2, name: 'city' },
  { id: 3, name: 'liverpool' }
];

for (var i = 0; i < clubs.length; i++) {
  if (clubs[i].id == 2) {
    clubs.splice(i, 1);
    break;
  }
}

console.log(clubs);
H77
  • 5,359
  • 2
  • 23
  • 38
0

Use Array.filter

var clubs = [ 
        {id: 1, name : 'chelsea'},
        {id: 2, name : 'city'},
        {id: 3, name : 'liverpool'}
    ];

clubs = clubs.filter(function(item){
   return item.id !== 2;
});

console.log(clubs);
Nikhil Aggarwal
  • 26,884
  • 3
  • 37
  • 53
  • 3
    @downvoter - Reason please? What's the point of down-vote if the reason is not specified? – Nikhil Aggarwal Apr 11 '18 at 05:07
  • 1
    Why not point to one of the many duplicate questions instead of repeating a solution here? Plenty of solutions for this on SO. – H77 Apr 11 '18 at 05:22
  • @H77 - The highest voted answer has a different approach, so wrote my approach as an answer. Fair I guess. – Nikhil Aggarwal Apr 11 '18 at 05:26
  • Probably better to add your solution there and mark this one as a duplicate. That way the answers wouldn't be scattered. – H77 Apr 11 '18 at 05:26
  • 1
    @nikhilagw I think this is the only decent answer suggested. I didn't downvote it. But I think H77 is right. – Aluan Haddad Apr 11 '18 at 05:27
  • @H77 - The general tendency is to follow the top answer. My answer there can be an option but to put it over here makes it more valuable. I believe there is difference in opinion on how to handle the situation. – Nikhil Aggarwal Apr 11 '18 at 05:28
  • 1
    It's the top answer [here](https://stackoverflow.com/questions/15287865/remove-array-element-based-on-object-property). Like I said.. plenty of duplicates. No reason to add more. – H77 Apr 11 '18 at 05:31
  • @H77 - Now the point is taken. However, you could see that there are still plenty of duplicates and you cannot do much about it. What really matters is how fast you get/search the solution or the proposed solution. It took some time of yours to get to the above link, however, 18 minutes back you had a link to a different approach. For me it was important to know the reason of down-vote, and I honor your reason but I have my own opinion about it and would like to stick to it. Thank you for all the clarification. – Nikhil Aggarwal Apr 11 '18 at 05:38
  • @H77 - Apologies for bringing this up again, however, in addition to above, it would be better to close the issue and mark it as duplicate rather than down-voting a correct possible solution. – Nikhil Aggarwal Apr 11 '18 at 05:46
  • @H77 - You made the right choice. :) Then may I request the down-voter to list his/her reason? – Nikhil Aggarwal Apr 11 '18 at 05:55
  • @downvoter - Reason please? What's the point of down-vote if the reason is not specified? – Nikhil Aggarwal Apr 11 '18 at 06:02
  • 1
    @nikhilagw Great. It works. Thanks a lot. I accept your answer and I voted for it. It's help me :) – Success Man Apr 11 '18 at 07:03
0

The element is not getting deleted because of your if statement, you are checking if 2 is present or not in array, but there is nothing like 2 in array as it is an array of objects. So the condition always remains false.

Ash
  • 423
  • 2
  • 10