忽然笑
2022-08-27 10:51:00
我有一個數組,我想從這個數組中刪除元素的子集。我嘗試使用循環和,但它沒有按預期工作。for 循環似乎不應該修改數組。有什么幫助嗎?INTERVALSforsplicefunction remove_intervals(list) { for(i=0; i < INTERVALS.length; i++) { var o = INTERVALS[i]; if(o in list) { clearInterval(o); INTERVALS.splice(i,1); } }}
2 回答

慕的地8271018
TA貢獻1796條經驗 獲得超4個贊
你可以像這樣使用:Array.prototype.filter
function remove_intervals(list) {
INTERVALS = INTERVALS.filter(id => {
if (id in list) {
clearInterval(id);
return false;
}
return true;
});
}

慕勒3428872
TA貢獻1848條經驗 獲得超6個贊
是否可以使用過濾器和includes方法來創建新數組。
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'];
添加回答
舉報
0/150
提交
取消