Reputation: 157
I have an array INTERVALS
and I want to remove a subset of elements from this array.
I tried using for
loop and splice
, but it is not working as desired. It seems the for loop should not modify the array. Any help?
function remove_intervals(list) {
for(i=0; i < INTERVALS.length; i++) {
var o = INTERVALS[i];
if(o in list) {
clearInterval(o);
INTERVALS.splice(i,1);
}
}
}
Upvotes: 0
Views: 270
Reputation: 571
Could you do this using a the filter and inccludes methods to create a new array
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const toRemove = ['limit', 'elite', 'destruction'];
const result = words.filter(word => toRemove.includes(word));
console.log(result);
// expected output: Array ['spray', 'exuberant', 'present'];
Upvotes: 1
Reputation: 22683
You can use Array.prototype.filter
like this:
function remove_intervals(list) {
INTERVALS = INTERVALS.filter(id => {
if (id in list) {
clearInterval(id);
return false;
}
return true;
});
}
Upvotes: 2