JavaScript Why does foreach skip one element from array

I am trying to iterate an array with forEach and, based on a condition, I do something.

For this question, I simplified the condition in order to try to understand what's going on.

I expect array b = [] after the operation, but it is not, as it operates only on half on the elements. Why does this happen?

(This is not about removing everything from b, just trying to understand why it jumps the even indexes).

var a = [1, 2, 3, 4, 5, 6];
var b = a.slice(0);

console.log('before b = ', b); // b = [1, 2, 3, 4, 5, 6]

a.forEach(function (e) {
    if (e > 0) {
        b.splice(a.indexOf(e), 1);
    }
});


console.log('after b = ', b); // b = [2, 4, 6]

// but I expect b = []
Answer:1

It does not. It goes through each and every item. The thing is what you do with array b. First you remove the index 0 from it which is 1. So now b = [2,3,4,5,6]. Then index 1 which is 3 so b = [2,4,5,6]. Then index 2 which is 5 so b = [2,4,6]. The next indexes don't exist so in the end b = [2,4,6].

To have the expected outcome use b.indexOf(e) in your splice call.

var a = [1, 2, 3, 4, 5, 6];
var b = a.slice(0);

console.log('before b = ', b); // b = [1, 2, 3, 4, 5, 6]

a.forEach(function (e) {
    if (e > 0) {
        b.splice(b.indexOf(e), 1);
    }
});


console.log('after b = ', b); // b = [2, 4, 6]

// but I expect b = []
Answer:2

You could take the index directly for splicing and make an log from the value an the array. You se, that the array becomes smaller than the index for splicing.

var a = [1, 2, 3, 4, 5, 6];
var b = a.slice(0);

console.log(b.join(' '));

a.forEach(function (e, i) {
    if (e > 0) {
        b.splice(a.indexOf(e), 1);
        console.log(i, ': ', b.join(' '));
    }
});

console.log(b); // b = [2, 4, 6]
Answer:3

I have a list of images like so: <div> <div class="imgCnr"> <img src="abc.jpg"> </div> <div class="imgCnr"> <img src="def.jpg"> </div&...

I have a list of images like so: <div> <div class="imgCnr"> <img src="abc.jpg"> </div> <div class="imgCnr"> <img src="def.jpg"> </div&...

  1. select parent element css
  2. select parent element javascript
  3. select parent element jquery
  4. select parent element xpath
  5. select parent element sass
  6. select parent element js
  7. select parent element using css
  8. select parent element selenium
  9. select parent element using javascript
  10. select parent element from child css
  11. select parent element
  12. select parent element scss
  13. select parent element css3
  14. select parent element with class
  15. select parent element angular
  16. select parent element using jquery
  17. select parent element html
  18. select parent element from iframe
  19. select parent element of class css
  20. select parent element angularjs

I am trying to call a function that isn't being recognised. I have a PHP block of code that adds the form to the HTML when the user is logged in: <?php if(isset($_SESSION['user'])) { $usr = $...

I am trying to call a function that isn't being recognised. I have a PHP block of code that adds the form to the HTML when the user is logged in: <?php if(isset($_SESSION['user'])) { $usr = $...

  1. onsubmit call javascript function
  2. onsubmit run javascript function
  3. onsubmit call js function
  4. onsubmit javascript call
  5. call onsubmit js
  6. html form onsubmit call javascript function
  7. html onsubmit call javascript function

I have used more effects.The effects likes based on the drag the scroll in browser or going to down of the page in my home page. I have implemented the top button, it can goes top of the page. When i ...

I have used more effects.The effects likes based on the drag the scroll in browser or going to down of the page in my home page. I have implemented the top button, it can goes top of the page. When i ...

I am new to React and I try to build a timer component. Now I have the start function working but I would also like to stop the timer by an onclick handler. The problem is that the start function uses ...

I am new to React and I try to build a timer component. Now I have the start function working but I would also like to stop the timer by an onclick handler. The problem is that the start function uses ...

  1. stop interval in javascript
  2. stop interval in js
  3. stop interval in angularjs
  4. stop interval in jquery
  5. setinterval react native
  6. stop interval in angular
  7. setinterval on click
  8. stop interval in function
  9. typescript setinterval
  10. angular 6 stop interval
  11. stop interval in function javascript
  12. js stop interval from inside