【问题标题】:How to loop through 2 arrays to create a new key/value object如何遍历 2 个数组以创建新的键/值对象
【发布时间】:2019-09-27 14:47:08
【问题描述】:

我有 2 个数组 - countryNames 和 countryCodes。这些数组中的项目的索引序列对齐,因为它们来自同一个 API,即 countryNames[0] 是阿富汗,countryCodes[0] 是“AF”等。

我正在尝试创建一个新的、单独的对象来将数据整齐地存储在键/值对中(就像 JSON 对象一样),但我还没有成功。有人建议遍历它们,但我不太确定该怎么做。任何帮助将不胜感激!

以下是我取得某种成功的唯一代码。它给了我一个对象(虽然看起来很奇怪),但它没有将数据存储在键/值对关系中。

 var keys = [];
 var values = [];


 fetch("https://restcountries.eu/rest/v2/all")
.then((response) => {
    return response.json();
})
.then((data) => {
    const codes = data.map(item => item.alpha2Code);
    values.push(codes);

    const names = data.map(item => item.name);
    keys.push(names);

    var result = [];
    keys.forEach((key, i) => result[key] = values[i]);
    console.log(result);
});

我只想拥有类似的东西 -
{
国家名称:国家代码,
2ndCountryName: 2ndCountryCode,
第三个国家名称:第三个国家代码,
等等.....
};

【问题讨论】:

  • 能否请您添加输入
  • 我知道有人问过类似的问题,并且我已经尝试了那里提出的所有解决方案 - 对我没有任何帮助,我不确定为什么。我已经设法使它与下面我标记为最有帮助的代码一起工作。谢谢大家!

标签: javascript arrays loops object


【解决方案1】:

Array.reduce 救援:

fetch("https://restcountries.eu/rest/v2/all")
  .then((response) => {
    return response.json();
  })
  .then((data) => {
      const result = data.reduce((countries, item)=> {
      	countries[item.name] = item.alpha2Code;
        return countries;
      }, {});
      console.log(result);
  });

如果您想了解更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

【讨论】:

    【解决方案2】:

    fetch("https://restcountries.eu/rest/v2/all").then((response) => {
    	return response.json();
    }).then((data) => {
    	var result = {}
    	for (var i = 0; i < data.length; i++) {
    		result[data[i].name] = data[i].alpha2Code;
    	}
    	console.log(result);
    });

    【讨论】:

      【解决方案3】:
      fetch("https://restcountries.eu/rest/v2/all")
      .then((response) => {
          return response.json();
      })
      .then((data) => {
        const results = data.reduce((agg, item) => {
          agg[item.name] = item.alpha2Code
          return agg
        }, {})
        console.log(results)
      })
      

      【讨论】:

      • 请不要只发布代码作为答案,而是说明您的代码的作用以及它如何解决问题。带有解释的答案通常质量更高,更有可能吸引投票。
      【解决方案4】:

      var keys = [];
       var values = [];
      
      
       fetch("https://restcountries.eu/rest/v2/all")
      .then((response) => {
          return response.json();
      })
      .then((data) => {
          const codes = data.map(item => item.alpha2Code);
          values.push(codes);
      
          const names = data.map(item => item.name);
          keys.push(names);
      
          var result = [];
          
          keys[0].forEach((key, i) => {
             const obj = {};
             obj[values[0][i]] = key;       
             result.push(obj);
           }
          );
          
          console.log(result);
      });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-12
        • 2020-11-15
        • 2019-12-18
        • 2019-02-12
        • 1970-01-01
        • 2018-04-12
        • 2020-10-02
        相关资源
        最近更新 更多