Short answer
Prefer using filter over a loop (unless performance is critical & the loop benchmarks to a meaningful gain over the Array filter method which doesn’t introduce state)
const xs = [ 1, 2, 3, 4, 5 ] // array of values
const ys = [ 1, 3 ] // indices to skip
// using `filter`
const zs = xs.filter((_, i) => !ys.includes(i))
console.log(zs)
//=> [ 1, 3, 5 ]
Long answer
When possible, you don’t want to be using a loop because it can hurt the ability to understand your code taking a procedural approach over a declarative approach. You can instead use a higher-order function like Array.prototype.filter to keep the values you want. The return value of the function passed into filter needs to return a boolean of what to keep. In JavaScript, the filter function is of binary arity, meaning it takes two arguments, with the first argument being the array element and the second being the index. We can ignore the array element value as it is only the index we need to check (this is why the function starts with (_, ...) to drop the first argument).
Array.prototype.includes is a function on the array prototype that lets you know if the array contains a value (i.e. [0].includes(0) === true and [0].includes(1) == false).
Putting these two concepts together, we can iterate over the first array, xs, ignoring the values but using the index to see if it's included in our blacklisted indices array, ys.
If we looked at this as a map function, instead of filter that included our value
xs.map((v, i) => [ v, !ys.includes(i) ])
//=> [ [ 1, true ], [ 2, false ], [ 3, true ], [ 4, false ], [ 5, true ] ]
We can see which values will be true. And if we remember, filter keeps our true values [ 1, 3, 5 ].
xand `y ?!