代码之家  ›  专栏  ›  技术社区  ›  Omar Morrison

Unnest对象数组,并将嵌套值转换为键/值对

  •  0
  • Omar Morrison  · 技术社区  · 3 年前

    嵌套阵列, 统计数据 ,由包含键值对的对象组成。

    • 每个 键值对 是一个具有两个元素的对象: 第一个元素 是吗 属性密钥 和 第二元素 是吗 财产价值 .
    • 这个 球门 是的 statName 和 statValue 转换为 关键字:值 最外层对象阵列中的对。

    我在MDN Web文档上发现了两个似乎有些相关的JavaScript函数: Object.formEntries() 和 Object.entries() .

    • 这个 Object.fromEntries() static方法将键值对列表转换为对象。
    • 这个 Object.entries() static方法返回给定对象自己的可枚举字符串键控属性键值对的数组

    [
      {
        "playerName": "Chris Kirk",
        "country": "United States",
        "countryFlag": "USA",
        "rank": 35,
        "stats": [
          {
            "statName": "Avg",
            "statValue": "70.356",
          },
          {
            "statName": "Total Strokes",
            "statValue": "5,176",
          },
          {
            "statName": "Total Adjustment",
            "statValue": "30.308",
          },
          {
            "statName": "Total Rounds",
            "statValue": "74",
          }
        ]
      },
      {
        "playerName": "Kevin Yu",
        "country": "Chinese Taipei",
        "countryFlag": "TPE",
        "rank": 36,
        "stats": [
          {
            "statName": "Avg",
            "statValue": "70.386",
          },
          {
            "statName": "Total Strokes",
            "statValue": "3,251",
          },
          {
            "statName": "Total Adjustment",
            "statValue": "57.157",
          },
          {
            "statName": "Total Rounds",
            "statValue": "47",
          }
        ]
      }
    ]
    

    我正在努力实现的目标。

    [
      {
        "playerName": "Chris Kirk",
        "country": "United States",
        "countryFlag": "USA",
        "rank": 35,
        "Avg": "70.356",
        "Total Strokes": "5,176",
        "Total Adjustment":"30.308",
        "Total Rounds": "74"
      },
      {
        "playerName": "Kevin Yu",
        "country": "Chinese Taipei",
        "countryFlag": "TPE",
        "rank": 36,
        "Avg": "70.386",
        "Total Strokes": "3,251",
        "Total Adjustment": "57.157",
        "Total Rounds": "47"
      }
    ]
    
    4 回复  |  直到 3 年前
        1
  •  1
  •   Mischa    3 年前

    const input = [
      {
        "playerName": "Chris Kirk",
        "country": "United States",
        "countryFlag": "USA",
        "rank": 35,
        "stats": [
          {
            "statName": "Avg",
            "statValue": "70.356",
          },
          {
            "statName": "Total Strokes",
            "statValue": "5,176",
          },
          {
            "statName": "Total Adjustment",
            "statValue": "30.308",
          },
          {
            "statName": "Total Rounds",
            "statValue": "74",
          }
        ]
      },
      {
        "playerName": "Kevin Yu",
        "country": "Chinese Taipei",
        "countryFlag": "TPE",
        "rank": 36,
        "stats": [
          {
            "statName": "Avg",
            "statValue": "70.386",
          },
          {
            "statName": "Total Strokes",
            "statValue": "3,251",
          },
          {
            "statName": "Total Adjustment",
            "statValue": "57.157",
          },
          {
            "statName": "Total Rounds",
            "statValue": "47",
          }
        ]
      }
    ]
    
    const output = input.map(({stats, ...rest}) => ({
        ...rest, 
        ...stats.reduce((acc, {statName, statValue}) => ({
            ...acc, 
            [statName]: statValue
        }), {})
    }))
    
    console.log(output)
        2
  •  0
  •   NIKUNJ KOTHIYA    3 年前

    您可以使用映射来修改旧的响应,在映射内部可以循环获取状态值

    const formattedResponse = originalResponse.map((item) => {
      // Create a new object to store
      const newObj = {
        playerName: item.playerName,
        country: item.country,
        countryFlag: item.countryFlag,
        rank: item.rank,
      };
    
      // Iterate through the 'stats' array and add the properties to the new object
      item.stats.forEach((stat) => {
        newObj[stat.statName] = stat.statValue;
      });
    
      return newObj;
    });
    
        3
  •  0
  •   Hedi Zitouni    3 年前

    这是你问题的有效解决方案

    const unnestData = data.map(d => {
      d.stats.forEach(({statName, statValue}) => d[statName] = statValue)
      delete d.stats
      return d
    })
    
        4
  •  0
  •   Peybro    3 年前

    @evolutionxbox怎么说。但这里有一个解决您问题的方法:

    const transformedData = data.map((player) => {
      const playerStats = {};
      player.stats.forEach((stat) => {
        playerStats[stat.statName] = stat.statValue;
      });
      delete player["stats"];
    
      return {
        ...player,
        ...playerStats
      };
    });
    

    跑步

    const data = [{
        "playerName": "Chris Kirk",
        "country": "United States",
        "countryFlag": "USA",
        "rank": 35,
        "stats": [{
            "statName": "Avg",
            "statValue": "70.356",
          },
          {
            "statName": "Total Strokes",
            "statValue": "5,176",
          },
          {
            "statName": "Total Adjustment",
            "statValue": "30.308",
          },
          {
            "statName": "Total Rounds",
            "statValue": "74",
          }
        ]
      },
      {
        "playerName": "Kevin Yu",
        "country": "Chinese Taipei",
        "countryFlag": "TPE",
        "rank": 36,
        "stats": [{
            "statName": "Avg",
            "statValue": "70.386",
          },
          {
            "statName": "Total Strokes",
            "statValue": "3,251",
          },
          {
            "statName": "Total Adjustment",
            "statValue": "57.157",
          },
          {
            "statName": "Total Rounds",
            "statValue": "47",
          }
        ]
      }
    ]
    
    const transformedData = data.map((player) => {
      const playerStats = {};
      player.stats.forEach((stat) => {
        playerStats[stat.statName] = stat.statValue;
      });
      delete player["stats"];
    
      return {
        ...player,
        ...playerStats
      };
    });
    
    console.log(transformedData);