1

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);

}
3
  • I would reword your description of the question and tell what is your desired output.. After the code block you provided is ran, the remaining elements in the array are 2,4,6,8,10 Commented Mar 21, 2020 at 1:12
  • @codeherk yeah you're right i wrote it wrong there, it suppose to be let i = 1 in loop to get the array left = [1,3,5,7,9] but my question is still the same could you please explain why after this cod ran the original array remain [1, 3, 5, 7, 9] and the removed item from the array = [2,4,6,8,10] Commented Mar 21, 2020 at 1:20
  • do you want to know why it's not recommended to alter an array that you're iterating over, or do you want help with what you're actually trying to accomplish? Commented Mar 21, 2020 at 1:44

4 Answers 4

2

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]

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for explaining on the point exactly where i need, now it completely clear. :)
0

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.

1 Comment

why does it remove [2,4,6,8,10] from the arr and remain [1,3,5,7,9] ?
0

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.

Comments

0

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);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.