gpt4 book ai didi

javascript - 用 `for` 循环替换 `forEach` 循环

转载 作者:行者123 更新时间:2023-11-30 06:21:02 27 4
gpt4 key购买 nike

我正在研究将任务卡移至下一列的 StateService 方法。我能够编写工作正常的 taskMoveLeft 方法,但我无法使用 forEach 循环为 taskMoveRight 方法复制它的功能,我可以让它只与 for 循环一起工作。

taskMoveLeft 方法的工作示例(使用 forEach):

taskMoveLeft(id) {
state.columns.forEach((column, columnIndex) => {
if (state.columns[0] !== column) {
if (column.cards) {
column.cards.forEach((card, cardIndex) => {
if (card.id === id) {
if (state.columns[columnIndex - 1].cards) {
// Add card to the target column card collection
state.columns[columnIndex - 1].cards.push(card);
} else {
// Create target column card collection and add card
state.columns[columnIndex - 1].cards = Array.of();
state.columns[columnIndex - 1].cards.push(card);
}
// Remove the card from the source column card collecion
state.columns[columnIndex].cards.splice(cardIndex, 1);
}
});
}
}
});
}

taskMoveRight 方法的工作示例(使用 for 循环):

taskMoveRight(id) {
for (let i = 0; i < state.columns.length; i++) {
if (state.columns[state.columns.length - 1] !== state.columns[i]) {
if (state.columns[i].cards) {
for (let j = 0; j < state.columns[i].cards.length; j++) {
if (state.columns[i].cards[j].id === id) {
if (state.columns[i + 1].cards) {
// Add card to the target column card collection
state.columns[i + 1].cards.push(state.columns[i].cards[j]);
} else {
// Create target column card collection and add card
state.columns[i + 1].cards = Array.of();
state.columns[i + 1].cards.push(state.columns[i].cards[j]);
}
// Remove the card from the source column card collecion
return state.columns[i].cards.splice(j, 1);
}
}
}
}
}
}

无法使 taskMoveRight 方法与 forEach 循环一起使用。使用此代码,卡片总是移动到最右边的列:

taskMoveRight(id) {
state.columns.forEach((column, columnIndex) => {
if (state.columns[state.columns.length - 1] !== column) {
if (column.cards) {
column.cards.forEach((card, cardIndex) => {
if (card.id === id) {
// Create target column card collection
if (!state.columns[columnIndex + 1].cards) {
state.columns[columnIndex + 1].cards = Array.of();
}
// Add card to the target column card collection
state.columns[columnIndex + 1].cards.push(card);
// Remove the card from the source column card collecion
state.columns[columnIndex].cards.splice(cardIndex, 1);
}
});
}
}
});
}

最佳答案

forEach 不是合适的工具,因为您提前终止了循环。相反,由于您需要卡片的索引,请使用 findIndex。请参阅 *** 评论和其他建议:

taskMoveRight(id) {
for (let i = 0; i < state.columns.length; i++) {
// *** Instead of repeating it all over
const column = state.columns[i];
if (state.columns[state.columns.length - 1] !== column) {
if (column.cards) {
// *** Find the card
const cardIndex = column.cards.findIndex(card => card.id == id);
// *** Get the card if found
const card = cardIndex !== -1 && column.cards[cardIndex];
if (card) {
// *** Instead of repeating it
const nextColumn = state.columns[i + 1];
if (nextColumn.cards) {
// Add card to the target column card collection
nextColumn.cards.push(card);
} else {
// Create target column card collection and add card
// *** Using Array.of() to create an empty array and then pushing
// *** to it is quite round-about; instead: [card]
nextColumn.cards = [card];
}
// Remove the card from the source column card collecion
return column.cards.splice(cardIndex, 1);
}
}
}
}
}

关于javascript - 用 `for` 循环替换 `forEach` 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53081493/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com