NathanP
NathanP

Reputation: 49

Using splice to delete item in array

Why does the remaining in original array = [1, 3, 5, 7, 9]

Since arr.splice(i, 1) = i is the target index and 1 is the number of item to be removed, i is increases to 10 respectively from i++ which short for i = i + 1, So why does it remove 5 index and remain 5 in the array ? that's what i know so far and i have struggled to read the docs but still have no idea to understand, please explain it for me

let arr = [1,2,3,4,5,6,7,8,9,10];

for(let i = 1; i < arr.length; i++) {
    arr.splice(i, 1);

}

Upvotes: 1

Views: 75

Answers (4)

Ali Belkadhi
Ali Belkadhi

Reputation: 1

You need to check the for loop end condition, i is not increasing to 10. Why? because i < arr.length. So it will like this :

Iteration 1:

i=0; arr.length = 10; arr = [1,2,3,4,5,6,7,8,9,10]; ==> result [2,3,4,5,6,7,8,9,10]; 

Iteration 2:

i=1; arr.length = 9; arr = [2,3,4,5,6,7,8,9,10];  ==> result [2,4,5,6,7,8,9,10]; 

Iteration 3:

i=2; arr.length = 8; arr = [2,4,5,6,7,8,9,10];  ==> result [2,4,6,7,8,9,10]; 

. . .and so forth

i = 5 ==> arr.length: 5 ==> final result : [2, 4, 6, 8, 10]

So if you want to delete all items using splice:

let arr = [1,2,3,4,5,6,7,8,9,10];

while(arr.length > 0) {

    arr.splice(0, 1);

}

Upvotes: 0

Jacebot
Jacebot

Reputation: 53

Remember, arrays are 0 based. Second, the length is changing each time it evaluates.

MDN links:

Splice

Map

So you may want to try

i =< length

Where length is determined and is set ahead of time and constant. You can try mapping the array to a new one, so the original array stays pure and unaffected by the splice.

Upvotes: 0

Matt B
Matt B

Reputation: 116

You're wondering why it's removing 1, 3, 5, 7, and 9, right?

Here's why. As the for loop iterates, i keeps increasing by one. HOWEVER, by calling .splice, you are removing the first element of the array, so as a result, every other element moves down an index.

Let's play this out step by step for a few iterations of the for loop.

i = 0; arr.splice(0, 1) removes 1, so arr is [2, 3, 4, 5, 6, 7, 8, 9, 10]

i = 1; arr.splice(1, 1) removes 3, not 2, because now 3 is at index 1 of arr. Performing the splice leaves arr as [2, 4, 5, 6, 7, 8, 9, 10].

i = 2; arr.splice(2, 1) removes 5, because 5 is currently at index 2. As a result, arr becomes [2, 4, 6, 7, 8, 9, 10].

Is it clear now what's going on?

If your goal is to successively remove each element, one at a time, then instead of calling .splice(i, 1) in each iteration of the loop, you should call .splice(0, 1), since the value at index 0 changes each time you call .splice.

Upvotes: 0

Kevin Lin
Kevin Lin

Reputation: 111

It is because the length of arr decreases everytime splice function runs. Here is how the array changes.

[2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 6, 7, 8, 9, 10]
[2, 4, 6, 8, 9, 10]
[2, 4, 6, 8, 10]

So every loop, i increases and arr.length decreases by 1. so only 5 loops runs and the result is [2, 4, 6, 8, 10]

Upvotes: 2

Related Questions