在reducer内,我正在调用reoder方法。 在方法内部,我尝试重新排列元素,并将startIndex处的元素移到endIndex处。
但是,当我放置调试器时,只要它到达
行,const [removed] = ...
它返回到函数调用,并多次调用/提供更慢功能。此后,即使cards array从不为空,tempArray也为空。
如果我删除该行,则tempArray不为空,并且与cards数组相同。但是,在那一行中,tempArray始终为空。
也是
const reoder = (cards, startIndex, endIndex) => {
debugger
const tempArray = Array.from(cards)
const [removed] = tempArray.splice(startIndex)
console.log(tempArray)
}
这是我的纸牌阵列。
mainPlayerCards: [
{
id: 0,
value: "A"
},
{
id: 1,
value: "B"
},
{
id: 2,
value: "C"
}
],
任何帮助将不胜感激。我不太确定那里发生了什么。
谢谢
答案 0 :(得分:0)
您必须将1
(要删除的项目数)作为第二个参数传递给tempArray.splice(startIndex)
调用:
const reoder = (cards, startIndex, endIndex) => {
const tempArray = Array.from(cards)
const [removed] = tempArray.splice(startIndex, 1)
tempArray.splice(endIndex, 0, removed);
console.log(tempArray);
return tempArray;
};
reoder([
{
id: 0,
value: "A"
},
{
id: 1,
value: "B"
},
{
id: 2,
value: "C"
}
], 0,1);