2 回答

TA貢獻1712條經驗 獲得超3個贊
在這種情況下,您需要做的是跟蹤應過濾掉哪些元素的計數,然后在過濾時更新該計數。不幸的是,我不清楚你的細胞數據,所以我繼續做出自己的解釋。
let data = [];
for (let i = 0; i < 100; i++) {
data.push({ id: i });
}
function getByNine(id) {
let count = id + 9
return data.filter(i => {
if (i.id === count) {
count = count + 9
return i
} else {
return
}
})
}
console.log(getByNine(3))
getByNine(i)
在事件偵聽器中,您可以放置對函數(或任何您想要調用它的名稱)的引用并傳遞其索引。在此函數中,您設置默認計數 9 + 無論單擊的元素的 id 是什么
對對象數組運行過濾器,當計數正確時返回該項目,然后將計數增加 9 以獲取下一個正確的元素
function getByNine(i) {
// code here
}
newCell.forEach((item, i) => {
item.addEventListener('click', () => {
getByNine(i)
})
})

TA貢獻1786條經驗 獲得超11個贊
我沒有包含“顯示”該系列中將出現的那些元素的代碼,但下面是記錄它們的代碼。您可以更新它以顯示它們。
const DIFF = 9;
newCell.forEach((item, i) => {
item.addEventListener('click', ()=>logAllItemsInSeries(i));
}
const logAllItemsInSeries = (i) => {
console.log(i);
const series = [];
while (i < newCell.length) {
series.push(i);
i += DIFF;
}
series.forEach((index) => {
console.log(index);
console.log(newCell[index]);
});
};
添加回答
舉報