【问题标题】:I am not able to splice the array - × TypeError: Cannot read property 'splice' of undefined我无法拼接数组 - × TypeError: Cannot read property 'splice' of undefined
【发布时间】:2021-03-12 23:25:57
【问题描述】:

我在拼接数组时遇到问题。我正在尝试拼接它以修改该数组中元素的位置,但它返回时出现错误。

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)

【问题讨论】:

    标签: javascript node.js reactjs


    【解决方案1】:

    我不太明白您在第 7 行 (newList[currentItem].splice(currentItem.idx, 1)[0]) 中要做什么。

    您正在定位索引为“currentItem”的元素并对其应用拼接。这不起作用,因为您的目标是单个元素而不是数组。

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 2021-09-25
      • 2017-04-24
      • 2023-03-15
      • 2020-10-10
      • 2023-02-22
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多