How to make my for loop skip index 1 and 2 from the combined arrays (javascript)

var x = [1,2,3,4,5,6,7,8,9]
var y = [1,2,3,4,5,6,7,8,9]
var z = [x,y]
z.forEach(element => {
for(var i=0;i<z.length;i++){
if( i ===1 || i===2 ){
continue;

} else{
    console.log(z[i]);
}

You’d test the index wouldn’t you? And if they’re different indexes then you’d test both?

Looks like you’re iterating through one, and then the other. So really, at any time you’re only iterating through one array, aren’t you? Not sure what you mean with combined.

Maybe you mean to get a product of the two arrays, like the coordinates for a grid. But if so, then you should probably make sure that you’re doing that before you start thinking about making skips. Solve one problem before moving on to the next one.

Thanks for the reply ionatan.
I am new to javascript I was actually getting weird infinite loop which made me to start trying things out of frustration . The below code actually worked now …maybe something is wrong with my laptop.
var x = [1,2,3,4,5,6,7,8,9]
var y = [1,2,3,4,5,6,7,8,9]
var z = [x,y]
z.forEach(element => {
for(var i=0;i<element.length;i++){
if( i ===1 || i===2 ){
continue;

} else{
    console.log(element[i]);
}
 


}

});

Well, it’s not the same code, so it’s not by some fluke that it behaves differently.
I think it should be written this way instead:

const x = [1,2,3,4,5,6,7,8,9]
const y = [1,2,3,4,5,6,7,8,9]

for (let i = 0; i < x.length; i++) {
  if (i != 1 && i != 2) {
    console.log(x[i])
  }
}

for (let i = 0; i < y.length; i++) {
  if (i != 1 && i != 2) {
    console.log(y[i])
  }
}

Or make a function to do the looping, and call it for x and then y (to avoid writing the same loop twice)

Why not just start your loop at i=2?

for (let i = 2; i < x.length; i++) {
    console.log(x[i]);
}

That’d skip the 0 as well, but I’d prefer a more data oriented/general solution too, but how exactly that’d look depends a bit on the context and would probably bring in more overhead than what’s going on so far

1 Like

Ah check I misinterpreted it. You need the 0 index as well ;).

Thanks Ionatan,
Assume if i have a thousand array, won’t i need at some point combine them instead of writing a thousand for loop or calling it a thousand times.
I will look into using function like you suggested .Thanks a lot

Deleting a range from an array is exactly what splice does:

> input = [10, 11, 12, 13, 14, 15]
[ 10, 11, 12, 13, 14, 15 ]
> input.splice(1, 2)
[ 11, 12 ]
> input
[ 10, 13, 14, 15 ]

splice returned the removed sub-array, and the original got modified.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

So you’d iterate through your arrays and do this on each one. Or, perhaps you want the original arrays to not be modified, in which case you would make copies first.

Or do it with only loops. Doesn’t really matter.

If you have a thousand of them then they’re already in an array aren’t they.

function dothethings(arrays) {
  for array in arrays {
    dosomething(array)
  }
}
function dosomething(array) {
  remove second and third item
}