提问者:小点点

无法拼接数组-×TypeError:无法读取未定义的属性“splice”


我在拼接阵列时遇到了问题。我试图拼接它来修改数组中元素的位置,但它返回时出现错误。

if (e.target !== dragNode.current) {
      setUrls((oldList) => {
        let newList = [...oldList];
        newList.splice(
          params.idx,
          0,
          newList[currentItem].splice(currentItem.idx, 1)[0]
        );
        dragItem.current = params;
        return newList;
      });
    }

其中旧列表为:

OLD LIST 
(2) ["x", "x"]
0: "x"
1: "x"
length: 2
__proto__: Array(0)

和新列表:

NEW LIST 
(2) ["x", "x"]
0: "x"
1: "x"
length: 2
__proto__: Array(0)

共1个答案

匿名用户

我不太理解您在第7行(newlist[currentItem].splice(currentItem.idx,1)[0])中试图做什么。

您的目标是索引为“Current Item”的元素,并对其应用拼接。这不起作用,因为您的目标是一个数组的单个元素。