【问题标题】:Cannot read properties of undefined (reading 'items') [closed]无法读取未定义的属性(读取“项目”)[关闭]
【发布时间】:2021-09-16 07:22:55
【问题描述】:

我还有其他类似的 JSON 数据,因此,我想对其进行过滤。

const data = [{
      "1": {items: { Part1: true, Part2: true } },
      id: "zaMR9TR7hNV3p3VFNumyNbXMto93",
      displayName: name1
    }]

但是,我不断收到错误

无法读取未定义的属性(读取“项目”)

  const filter = data.filter(
    (a) =>
      a.["1"].items.Part1 == true
  );

我该如何解决这个问题?

【问题讨论】:

    标签: javascript reactjs json


    【解决方案1】:

    你有一个额外的“。”就在 ["1"]

    之前
    a["1"].items.Part1
    

    顺便说一句,如果我是你,我会用“item”重命名项目,因为它不是一个数组,只是一个对象。

    【讨论】:

      【解决方案2】:

      您错误地访问了 elemet。请使用这个

       const filter = data.filter(
          (a) =>
            a["1"].items.Part1 == true
        );
      

      错误就在这里,a => a.["1"]后面的多余的点(.),应该是a["1"]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-17
        • 2019-04-09
        • 2019-09-29
        • 1970-01-01
        • 1970-01-01
        • 2018-10-22
        • 1970-01-01
        • 2022-06-19
        相关资源
        最近更新 更多