Karolis Vaitkevicius
Karolis Vaitkevicius

Reputation: 340

How to remove the next element after the first?

The goal was to get every other element of an array that matches the condition I had, but I couldn't get to do that.

So, I set off to try on another example which is found in the Array.prototype.indexOf() at MDN.

var beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];

beasts.splice(beasts.indexOf("bison",beasts.indexOf("bison"+1)),1);
console.log(beasts);

I'd expect it to remove the second "bison" from the array and yet it removes the last element which is "duck"...

What could I do here? (granted I might have not yet learned proper syntax for this stuff)

Upvotes: 4

Views: 125

Answers (5)

Sukanya Purushothaman
Sukanya Purushothaman

Reputation: 951

There are some little mistakes in your code which leads to your output.
First, of all to remove an element from an array we can use the splice() method as follows.

array.splice(starting_index_from_which_the elements_are_to_be_deleted, number_of_elements_to_be_deleted)

If greater than the length of the array, the start will be set to the length of the array.
The correct format of your code is:

var beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];

beasts.splice(beasts.indexOf("bison"),beasts.indexOf("bison")+1);
console.log(beasts);

Upvotes: 2

AzmahQi
AzmahQi

Reputation: 378

The problem is you are going for bison+1 not bison so try this:

var beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];

beasts.splice(beasts.indexOf("bison",beasts.indexOf("bison")),1);
console.log(beasts);

Upvotes: 3

OliverRadini
OliverRadini

Reputation: 6467

Your splicing code has only one error, which has been picked up in the comments (It should be: beasts.indexOf("bison") + 1 and not beasts.indexOf("bison"+1) as @adiga says).

My guess is, however, that you'd actually like to remove all instances of 'bison' from the list. I may be wrong there, but it's a guess. You can do that with a filter:

var beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];

const updatedBeasts = beasts.filter(beast => beast !== 'bison');

console.dir(updatedBeasts);

Upvotes: 4

Code Maniac
Code Maniac

Reputation: 37755

You need to fix your indexOf syntax

beasts.indexOf("bison"+1)  // this searches for `bison1`

to this

beasts.indexOf("bison") + 1

var beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];

beasts.splice(beasts.indexOf("bison",beasts.indexOf("bison")+1),1)
console.log(beasts);

Upvotes: 2

Chanaka Weerasinghe
Chanaka Weerasinghe

Reputation: 5742

here is the working way

let beasts = ['ant', 'bison', 'camel', 'duck', 'bison', 'duck', 'duck', 'bison', "duck", 'bison',"camel", "duck", "duck"];

beasts.splice(beasts.indexOf("bison",beasts.indexOf("bison")),1);
console.log(beasts);

Upvotes: 0

Related Questions