1

I have a JavaScript array of length 3

arr = [1, 3, 8];

Is there a way to add elements to arr while I am iterating over it? I want to insert two values 10 and 20 into the existing arr. Can I do it like this? The newly added elements, 10 and 20, must also be looped over in the same for in loop.

for(var i in arr) {
  if( i == 0 ) {
   arr[length + 0] = 10;
   arr[length + 1] = 20;
  }
}

Or, what is the correct way to add elements to an array while iterating and making sure that the newly added elements will also be looped over?

6
  • Use the other for loop for(var i = 0; i < arr.length; i++) and use arr.push(10); arr.push(20); instead of arr[length+0] Commented Sep 30, 2017 at 14:38
  • 2
    for (var ... in ...) is only meant for objects: Why is using “for…in” with array iteration a bad idea? Commented Sep 30, 2017 at 14:40
  • Do the additional elements need to be added during iteration? Why can't they be added first? Commented Sep 30, 2017 at 15:00
  • 1
    @AlanLarimer Most likely he's simplified the code in the question, and the real code is not adding constants like this. Commented Sep 30, 2017 at 15:07
  • 1
    You should not use a for loop to mutate the entity you are playing with in computer science. You best never do it but if you really need to... a) prepare your array in advance and then iterate by a for loop, b) modify it in a while loop c) modify it through recursion. Commented Sep 30, 2017 at 17:59

3 Answers 3

2

You could use a for statement and check the length while iterating the array.

With for ... in statement, iteration of new elements is not granted.

Properties added to the object over which iteration is occurring may either be visited or omitted from iteration. In general it is best not to add, modify or remove properties from the object during iteration, other than the property currently being visited.

var array = [1, 3, 8],
    i;

for (i = 0; i < array.length; i++) {
    console.log(array[i]);
    if (i === 0) {
        array.push(10, 20);
    }
}

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

3 Comments

Do the additional elements need to be added during iteration? Why can't they be added first?
@AlanLarimer, the op stated it. if added before iteration, it would not need some change, beside of the critical use of for ... in.
Sorry. Accidentally commented on your answer, meant to comment on OP.
1

You really shouldn't ever need to do this, there's probably a better method. But, if you absolutely have to, use a while loop instead:

var arr = [1, 2, 3];
var i = 0;
while (i < arr.length) {
  if( i == 0 ) {
    arr.push(10, 20);
  }
  console.log(arr[i]); // 1, 2, 3, 10, 20
  i++;
}

2 Comments

The more idiomatic way to add to an array is arr.push(10).
yep, was in a rush so I had just copied his code. Will edit
0

I can understand that the OA wants to test the elements one at a time in the array, then append new elements to the array (to also test) when particular conditions are met.
i have not verified this in js, but most languages I have used allow you to add new elements to the end of the array at any step of a for loop.

I have used the same process for linearizing data trees into arrays.

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.