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

时间:2017-05-10 23:59:52

标签: javascript arrays object mapreduce

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

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

另请参阅此codepen 关于如何实现这一点的任何建议我将非常感激。

非常感谢

1 个答案:

答案 0 :(得分:0)

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