【问题标题】:How to filter from an object by iterating over it in js如何通过在js中迭代对象来过滤对象
【发布时间】:2021-12-25 20:26:18
【问题描述】:

我试图通过迭代从对象中获取“类型”的值。对象看起来像这样。

{
  "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 = 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(" "),
  };
}

但是,我想在“列表”上使用另一个过滤器来过滤非空列表。那么我该如何实现呢。

【问题讨论】:

    标签: javascript arrays json sorting object


    【解决方案1】:

    您要检查 'list' 键是否存在于 team.table JSON 对象中

    你可以检查

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

    代码是

    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(" "),
         };
      }
    }
    

    【讨论】:

      【解决方案2】:

      1)您可以使用flatMapmap 获取所有type

      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);

      2) 使用forEach 并解构为:

      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);

      【讨论】:

        【解决方案3】:

        使用forEach 会更简洁。 由于您的数据结构,您将需要 2 个 forEach。 但下面的代码会:

        • 检查awards是否为空
        • 检查awards.type是否为空

        const data = {
          "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
                    },
                    {
                      "name": "Dave",
                      "school": "y",
                      "awards": {
                        "type": "gold"
                      },
                      "year": 2016
                    }
                  ]
                }
              },
              {
                "cityCode": 444,
                "list": {
                  "players": [
                    {
                      "name": "James",
                      "school": "y",
                      "awards": {
                        "type": null
                      },
                      "year": 2016
                    }
                  ]
                }
              },
              
              {
                "cityCode": 555,
                "list": {
                  "players": [
                    {
                      "name": "Name 101",
                      "school": "y",
                      "awards": {
                        "type": "platinum"
                      },
                      "year": 2016
                    },
                    {
                      "name": "Name 102",
                      "school": "y",
                      "awards": {
                        "type": null
                      },
                      "year": 2016
                    },
                    {
                      "name": "Name 103",
                      "school": "y",
                      "awards": null,
                      "year": 2016
                    },
                    
                  ]
                }
              }
            ]
          }
        }
        
        // Expanded your data with more items
        const data1 = data.team.table;
        
        let types = []
        data1.forEach((item, index)  => {
          
          item.list.players.forEach((player) => {
            const awards = player.awards;
            if (awards !== null && awards.type !== null) {
                types = [...types, awards.type];
            }
          })
          
        })
        
        // Get the list of types
        console.log(types);
        
        // Get unique list of types
        let unique_types = [...new Set(types)]
        console.log(unique_types);

        【讨论】:

          猜你喜欢
          • 2020-07-24
          • 2017-06-14
          • 2020-07-25
          • 1970-01-01
          • 2017-04-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-18
          相关资源
          最近更新 更多