Home > Mobile >  How to filter from an object by iterating over it in js
How to filter from an object by iterating over it in js

Time:11-14

I am trying to get the value of "type" from the object by iterating over it. The object looks like this.

{
  "team": {
    "table": [
      {
        "cityCode": 123,
        "list": {
          "players": [
            {
              "name": "peter",
              "school": "x",
              "awards": {
                "type": "gold"
              },
              "year": 2019
            }
          ]
        }
      },
      {
        "cityCode": 456,
        "list": {
          "players": [
            {
              "name": "Dave",
              "school": "y",
              "awards": {
                "type": "silver"
              },
              "year": 2018
            }
          ]
        }
      }
    ]
  }
}

I am able to get the type values using this:

const table = team.table;
for (let i = 0; i < table.length; i  ) {
  const values = {
    type: table[i].list.players
      .filter((a) => a.awards != null)
      .map((a) => a.awards.type)
      .join(" "),
  };
}

However, I want to use another filter on the "list" to filter non null lists. So how can I achieve that.

CodePudding user response:

You want to check Check if 'list' key exists inside a team.table JSON object

you can check by

if(table[i].hasOwnProperty('list')){

}

code is

const table = team.table;
for (let i = 0; i < table.length; i  ) {
if(table[i].hasOwnProperty('list')){
     const values = {
       type: table[i].list.players
         .filter((a) => a.awards != null)
         .map((a) => a.awards.type)
         .join(" "),
     };
  }
}

CodePudding user response:

1) You can get all type using flatMap and map as:

obj.team.table.flatMap((o) => o.list.players.map((o) => o.awards.type))

const obj = {
  team: {
    table: [
      {
        cityCode: 123,
        list: {
          players: [
            {
              name: "peter",
              school: "x",
              awards: {
                type: "gold",
              },
              year: 2019,
            },
          ],
        },
      },
      {
        cityCode: 456,
        list: {
          players: [
            {
              name: "Dave",
              school: "y",
              awards: {
                type: "silver",
              },
              year: 2018,
            },
          ],
        },
      },
    ],
  },
};

const types = obj.team.table.flatMap((o) => o.list.players.map((o) => o.awards.type));
console.log(types);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

2) Using forEach and destructuring as:

const obj = {
  team: {
    table: [
      {
        cityCode: 123,
        list: {
          players: [
            {
              name: "peter",
              school: "x",
              awards: {
                type: "gold",
              },
              year: 2019,
            },
          ],
        },
      },
      {
        cityCode: 456,
        list: {
          players: [
            {
              name: "Dave",
              school: "y",
              awards: {
                type: "silver",
              },
              year: 2018,
            },
          ],
        },
      },
    ],
  },
};

const table = obj.team.table;
const types = [];
for (let i = 0; i < table.length; i  ) {
  const { list: { players } } = table[i]
  players.forEach(({ awards: { type }}) => types.push(type))
}

console.log(types);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related