【问题标题】:How can I replace multiple object properties inside array如何替换数组中的多个对象属性
【发布时间】:2021-11-07 19:43:09
【问题描述】:

我正在编写一个 NODE.JS 代码,该代码向用户提供有关他在游戏中的帐户的一些数据。

它向游戏 API 发送一个 GET 请求,该 API 返回一个数组,其中包含玩家在游戏中每个“冠军”所拥有的“积分”数量。问题是,它不返回“冠军”的名字,只返回他们的 ID:

 [
      {
        championId: 19,
        championLevel: 7,
        championPoints: 116531,
      },
      {
        championId: 24,
        championLevel: 6,
        championPoints: 67710,
      },
      {
        championId: 131,
        championLevel: 6,
        championPoints: 67233,
      }
    ]

我有一个包含所有 ID 及其相关英雄名称的列表,如下所示。我想知道是否有办法用相应的冠军名称替换所有冠军 ID,所以我可以用冠军名称而不是 ID 打印数组,以便更容易理解信息。我在这里只包括了 3 个冠军,以使其更简单,但真正的数组有 150 个冠军,所以我需要一种“批量编辑”它的方法。

championId: 19 equals to champion name Warwick
championId: 24 equals to champion name Jax
championId: 131 equals to champion name Diana

我想打印的是这样的:

 [
      {
        championName: Warwick,
        championLevel: 7,
        championPoints: 116531,
      },
      {
        championName: Jax,
        championLevel: 6,
        championPoints: 67710,
      },
      {
        championName: Diana,
        championLevel: 6,
        championPoints: 67233,
      }
    ]

【问题讨论】:

    标签: node.js arrays object replace


    【解决方案1】:

    你要使用的是Array.prototype.mapMDN。它使用回调函数转换数组的所有元素并返回转换后的数组:

    const champions =  [
      {
        championId: 19,
        championLevel: 7,
        championPoints: 116531,
      },
      {
        championId: 24,
        championLevel: 6,
        championPoints: 67710,
      },
      {
        championId: 131,
        championLevel: 6,
        championPoints: 67233,
      }
    ];
    
    function getName(championId) {
      // your logic to get the name of the champion
    } 
    
    const newChampions = champions.map((c) => ({
      championName: getName(c.championId),
      championLevel: c.championLevel,
      championPoints: c.championPoints
    }));
    

    【讨论】:

      【解决方案2】:

      两个有用的数组函数是mapfind; map 对 score 数组进行变换,find 进行变换的重要部分,即在 names 数组中查找名称....

      let scores = [{
          championId: 19,
          championLevel: 7,
          championPoints: 116531,
        },
        {
          championId: 24,
          championLevel: 6,
          championPoints: 67710,
        },
        {
          championId: 131,
          championLevel: 6,
          championPoints: 67233,
        }
      ];
      
      let names = [{
          championId: 19,
          name: 'Warwick'
        },
        {
          championId: 24,
          name: 'Jax'
        },
        {
          championId: 131,
          name: 'Diana'
        }
      ];
      
      let scoresWithNames = scores.map(s => {
         // find the matching championId in the names array
         let name = names.find(n => n.championId === s.championId).name;
         return { ...s, name };
      });
      
      console.log(scoresWithNames);

      【讨论】:

        猜你喜欢
        • 2022-08-18
        • 2022-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多