2 回答

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
在這種情況下,您需要做的是跟蹤應(yīng)過(guò)濾掉哪些元素的計(jì)數(shù),然后在過(guò)濾時(shí)更新該計(jì)數(shù)。不幸的是,我不清楚你的細(xì)胞數(shù)據(jù),所以我繼續(xù)做出自己的解釋。
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)
在事件偵聽(tīng)器中,您可以放置對(duì)函數(shù)(或任何您想要調(diào)用它的名稱)的引用并傳遞其索引。在此函數(shù)中,您設(shè)置默認(rèn)計(jì)數(shù) 9 + 無(wú)論單擊的元素的 id 是什么
對(duì)對(duì)象數(shù)組運(yùn)行過(guò)濾器,當(dāng)計(jì)數(shù)正確時(shí)返回該項(xiàng)目,然后將計(jì)數(shù)增加 9 以獲取下一個(gè)正確的元素
function getByNine(i) {
// code here
}
newCell.forEach((item, i) => {
item.addEventListener('click', () => {
getByNine(i)
})
})

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
我沒(méi)有包含“顯示”該系列中將出現(xiàn)的那些元素的代碼,但下面是記錄它們的代碼。您可以更新它以顯示它們。
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]);
});
};
添加回答
舉報(bào)