Home > Back-end >  Cannot read properties of undefined (reading 'items')
Cannot read properties of undefined (reading 'items')

Time:09-16

I have other JSON data similar to this, hence, I would like to filter it.

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

However, I keep getting the error of

Cannot read properties of undefined (reading 'items')

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

How do I fix this?

CodePudding user response:

you have an extra "." right before ["1"]

a["1"].items.Part1

By the way, if i were you, i would rename items by "item", because it's not an array, just an object.

CodePudding user response:

You are accessing elemet incorrectly. please use this

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

error was here, the extra dot(.) after a => a.["1"], it should be a["1"]

  • Related