Javascript 将一个对象数组转换为另一个具有不同结构的对象数组

Javascript 将一个对象数组转换为另一个具有不同结构的对象数组,javascript,arrays,object,mapreduce,Javascript,Arrays,Object,Mapreduce,我有一个具有此结构的对象数组: let countries = [ { "country": "Aruba", "country_key": "ABW", "continent": "South America", "entries": [ { "year": "2001", "import": "1340

我有一个具有此结构的对象数组:

    let countries = [
       {
          "country": "Aruba",
          "country_key": "ABW",
          "continent": "South America",
          "entries": [
             {
                "year": "2001",
                "import": "134000",
                "export": "0"
             },
             {
                "year": "2002",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2003",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2004",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2005",
                "import": "3400000",
                "export": "0"
             }
          ]
       },
       {
          "country": "Afghanistan",
          "country_key": "AFG",
          "continent": "Asia",
          "entries": [
             {
                "year": "2001",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2002",
                "import": "34000000",
                "export": "0"
             },
             {
                "year": "2003",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2004",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2005",
                "import": "35000000",
                "export": "0"
             }
          ]
       },
         {
        "country": "Argentina",
        "country_key": "ARG",
        "continent": "South America",
        "entries": [
           {
              "year": "2001",
              "import": "6000000",
              "export": "6000000"
           },
           {
              "year": "2002",
              "import": "16000000",
              "export": "0"
           },
           {
              "year": "2003",
              "import": "12000000",
              "export": "0"
           },
           {
              "year": "2004",
              "import": "169000000",
              "export": "0"
           },
           {
              "year": "2005",
              "import": "3000000",
              "export": "0"
           }
        ]
        }
     ];
[
     {
      "year": "2001",
       "entries": [
            {
                "continent": "South America",
                "country": "Aruba",
                "import": "134000",
                "export": "0"
            },
            {
                "continent": "Asia",
                "country": "Afghanistan",
                "import": "0",
                "export": "0"
            },
            {
                "continent": "South America",
                "country": "Argentina",
                "import": "6000000",
                "export": "6000000"
            }
        ]
      },
      {
      "year": "2002",
       "entries": [
            {
                "continent": "South America",
                "country": "Aruba",
                "import": "0",
                "export": "0"
            },
            {
                "continent": "Asia",
                "country": "Afghanistan",
                "import": "34000000",
                "export": "0"
            },
            {
                "continent": "South America",
                "country": "Argentina",
                "import": "16000000",
                "export": "0"
            }
        ]
      }
    ]
我想把它转换成另一个具有这种结构的对象数组:

    let countries = [
       {
          "country": "Aruba",
          "country_key": "ABW",
          "continent": "South America",
          "entries": [
             {
                "year": "2001",
                "import": "134000",
                "export": "0"
             },
             {
                "year": "2002",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2003",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2004",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2005",
                "import": "3400000",
                "export": "0"
             }
          ]
       },
       {
          "country": "Afghanistan",
          "country_key": "AFG",
          "continent": "Asia",
          "entries": [
             {
                "year": "2001",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2002",
                "import": "34000000",
                "export": "0"
             },
             {
                "year": "2003",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2004",
                "import": "0",
                "export": "0"
             },
             {
                "year": "2005",
                "import": "35000000",
                "export": "0"
             }
          ]
       },
         {
        "country": "Argentina",
        "country_key": "ARG",
        "continent": "South America",
        "entries": [
           {
              "year": "2001",
              "import": "6000000",
              "export": "6000000"
           },
           {
              "year": "2002",
              "import": "16000000",
              "export": "0"
           },
           {
              "year": "2003",
              "import": "12000000",
              "export": "0"
           },
           {
              "year": "2004",
              "import": "169000000",
              "export": "0"
           },
           {
              "year": "2005",
              "import": "3000000",
              "export": "0"
           }
        ]
        }
     ];
[
     {
      "year": "2001",
       "entries": [
            {
                "continent": "South America",
                "country": "Aruba",
                "import": "134000",
                "export": "0"
            },
            {
                "continent": "Asia",
                "country": "Afghanistan",
                "import": "0",
                "export": "0"
            },
            {
                "continent": "South America",
                "country": "Argentina",
                "import": "6000000",
                "export": "6000000"
            }
        ]
      },
      {
      "year": "2002",
       "entries": [
            {
                "continent": "South America",
                "country": "Aruba",
                "import": "0",
                "export": "0"
            },
            {
                "continent": "Asia",
                "country": "Afghanistan",
                "import": "34000000",
                "export": "0"
            },
            {
                "continent": "South America",
                "country": "Argentina",
                "import": "16000000",
                "export": "0"
            }
        ]
      }
    ]
到目前为止,我已经尝试了以下代码:

    let array1 = countries.map(function(countryList) {
        return countryList.entries.map(function(entry) {
            return {year: entry.year, import: entry.import, export: entry.export}
        });
    });

    let array2 = array1.map(function(arr) {
        return arr.map(function (subarr) {
            return {year: subarr.year, 
                entries: countries.map(function(countryList) {
                return {
                    continent: countryList.continent,
                    country: countryList.country,
                    import: subarr.import,
                    export: subarr.export
                };
            })
            };
        });
    });
请看这个 任何关于如何实现这一目标的建议,我都将不胜感激

非常感谢

const stats=[];
const stats = [];

function yearNotCreated(year) {
    let found = true;
    if(stats.filter(stat => stat.year === year).length) {
        found = false;
    }
    return found;
}

function getYearIndex(year) {
    let yearIndex;
    for(let i = 0; i < stats.length; i++) {
        if (stats[i].year === year) {
            yearIndex = i;
            break;
        }
    }
    return yearIndex;
}


countries.forEach((country) => {
     country.entries.forEach(entry => {
          const newEntry = {
             continent: country.continent,
             country: country.country,
             "import": entry.import,
             "export": entry.export
         };
         if (yearNotCreated(entry.year)) {
             const yearEntry = {
                 year: entry.year,
                 entries: []
             };
             yearEntry.entries.push(newEntry);
             stats.push(yearEntry);
         } else {
            stats[getYearIndex(entry.year)].entries.push(newEntry);
         }
     });
});
console.log(stats);
已创建的功能(年){ 让发现=真实; if(stats.filter(stat=>stat.year==year.length){ 发现=错误; } 发现退货; } 函数getYearIndex(年){ 让我们看看指数; for(设i=0;i{ country.entries.forEach(条目=>{ 常量newEntry={ 大陆:国家,大陆, 国家:国家,国家, “导入”:entry.import, “导出”:entry.export }; 如果(已创建(条目年份)){ const yearEntry={ 年份:entry.year, 条目:[] }; yearEntry.entries.push(newEntry); 统计数据推送(年度输入); }否则{ 统计数据[getYearIndex(entry.year)].entries.push(newEntry); } }); }); console.log(stats);
到目前为止,我已经试过这个代码了
-它有效吗?如果没有,它是如何失败的?非常感谢@Anselm:)