# 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
}
``````