【问题标题】:How to group and transform an array based on multiple keys in javascript?如何根据javascript中的多个键对数组进行分组和转换?
【发布时间】:2019-07-16 05:52:23
【问题描述】:

您好,我是 javascript 新手,我想在项目中将数组的格式更改为新格式。这是我的代码

var array=[      
  {  Indicator: "Population National (% of population)", Country: "India", Year: "2012", Value: "98.8" },
  {  Indicator: "Population National (% of population)", Country: "India", Year: "2013", Value: "99.1" },
  {  Indicator: "Population National (% of population)", Country: "Japan", Year: "2012", Value: "99.2" },
  {  Indicator: "Population National (% of population)", Country: "Japan", Year: "2013", Value: "99.3" },
  {  Indicator: "Population National (% of population)", Country: "China", Year: "2012", Value: "99.4" },
  {  Indicator: "Population National (% of population)", Country: "China", Year: "2013", Value: "99.8" },

  {  Indicator: "Population Rural (% of population)", Country: "India", Year: "2012", Value: "97.9" },
  {  Indicator: "Population Rural (% of population)", Country: "India", Year: "2013", Value: "98.2" },
  { Indicator: "Population Rural (% of population)", Country: "Japan", Year: "2012", Value: "98.4" },
  {  Indicator: "Population Rural (% of population)", Country: "Japan", Year: "2013", Value: "98.7" },
  {  Indicator: "Population Rural (% of population)", Country: "China", Year: "2012", Value: "99.0" },
  {  Indicator: "Population Rural (% of population)", Country: "China", Year: "2013", Value: "98.0" },

  {  Indicator: "Population Urban (% of population)", Country: "India", Year: "2012", Value: "99.3" },
  {  Indicator: "Population Urban (% of population)", Country: "India", Year: "2013", Value: "93.6" },
  {  Indicator: "Population Urban (% of population)", Country: "Japan", Year: "2012", Value: "99.6" },
  {  Indicator: "Population Urban (% of population)", Country: "Japan", Year: "2013", Value: "97.6" },
  { Indicator: "Population Urban (% of population)", Country: "China", Year: "2012", Value: "95.6" },
  { Indicator: "Population Urban (% of population)", Country: "China", Year: "2013", Value: "99.6" }];

我需要修改如下

    var array=[{"Country":"India", "Indicator": "Population National (% of population)","2012": "98.8","2013": "99.1"},
 {"Country":"India", "Indicator": "Population Rural (% of population)","2012": "97.9","2013": "98.2"},
 {"Country":"India", "Indicator": "Population Urban (% of population)","2012": "99.3","2013": "93.6"},
 {"Country":"Japan", "Indicator": "Population National (% of population)","2012": "99.2","2013": "99.3"},
 {"Country":"Japan", "Indicator": "Population Rural (% of population)","2012": "98.4","2013": "98.7"},
 {"Country":"Japan", "Indicator": "Population Urban (% of population)","2012": "99.6","2013": "97.6"},

 {"Country":"China", "Indicator": "Population National (% of population)","2012": "99.4","2013": "99.8"},
 {"Country":"China", "Indicator": "Population Rural (% of population)","2012": "99.0","2013": "98.0"},
 {"Country":"China", "Indicator": "Population Urban (% of population)","2012": "95.6","2013": "99.6"}
];

我尝试了每种方法并循环遍历数组以创建新数组,但我无法完成所需的格式。

这就是我所做的

var excelArr = [],tempCountryArr=[],tempIndicArr=[],tempYearArr=[];

_.each(array, function (val, i) {
  if (_.contains(tempCountryArr, val.Country) == false) {
    tempCountryArr.push(val.Country);
  }
  if (_.contains(tempIndicArr, val.Indicator) == false) {
    tempIndicArr.push(val.Indicator);
  }
  if (_.contains(tempYearArr, val.Year) == false) {
    tempYearArr.push(val.Year);
  }
});
for (var i = 0; i < tempCountryArr.length; i++) {
  var countrydata = _.filter(array, function (item) {
    return (item.Country == tempCountryArr[i] && item.Indicator == tempIndicArr[i]);
  });

  var indicator = [];
  for (var j = 0; j < countrydata.length; j++) {

    for (var k = 0; k < tempYearArr.length; k++) {
      var yrArr = "";
      var yeardata = _.filter(countrydata, function (item) {
        return (item.Year == tempYearArr[k] && item.Indicator == countrydata[j].Indicator && item.Country == tempCountryArr[i]);
      });
      yrArr = tempYearArr[k];
      if (yeardata.length > 0) {
        if (yeardata[0].Value != null && yeardata[0].Value != undefined && yeardata[0].Value != "NaN") {

          if (excelArr.length != 0 && excelArr[j] != undefined) {
            excelArr[j][yrArr] = yeardata[0].Value;
          } else {
            excelArr.push({ [yrArr]: yeardata[0].Value });
          }

        } else {
          if (excelArr.length != 0 && excelArr[j] != undefined) {
            excelArr[j][yrArr] = "NA";
          }
          else {
            excelArr.push({ [yrArr]: "NA" });
          }

        }
      }
      else {
        if (excelArr.length != 0 && excelArr[j] != undefined) {
          excelArr[j][yrArr] = "NA";
        }
        else {
          excelArr.push({ yrArr: "NA" });
        }

      }
    }
    excelArr[j].Indicator = countrydata[j].Indicator.toString();
    excelArr[j]["Country"] = countrydata[j].Country;
  }
}

我正在寻求任何帮助。在此先感谢

【问题讨论】:

    标签: javascript jquery arrays json


    【解决方案1】:

    您可以像这样使用reduceObject.values

    var array=[{Indicator:"Population National (% of population)",Country:"India",Year:"2012",Value:"98.8"},{Indicator:"Population National (% of population)",Country:"India",Year:"2013",Value:"99.1"},{Indicator:"Population National (% of population)",Country:"Japan",Year:"2012",Value:"99.2"},{Indicator:"Population National (% of population)",Country:"Japan",Year:"2013",Value:"99.3"},{Indicator:"Population National (% of population)",Country:"China",Year:"2012",Value:"99.4"},{Indicator:"Population National (% of population)",Country:"China",Year:"2013",Value:"99.8"},{Indicator:"Population Rural (% of population)",Country:"India",Year:"2012",Value:"97.9"},{Indicator:"Population Rural (% of population)",Country:"India",Year:"2013",Value:"98.2"},{Indicator:"Population Rural (% of population)",Country:"Japan",Year:"2012",Value:"98.4"},{Indicator:"Population Rural (% of population)",Country:"Japan",Year:"2013",Value:"98.7"},{Indicator:"Population Rural (% of population)",Country:"China",Year:"2012",Value:"99.0"},{Indicator:"Population Rural (% of population)",Country:"China",Year:"2013",Value:"98.0"},{Indicator:"Population Urban (% of population)",Country:"India",Year:"2012",Value:"99.3"},{Indicator:"Population Urban (% of population)",Country:"India",Year:"2013",Value:"93.6"},{Indicator:"Population Urban (% of population)",Country:"Japan",Year:"2012",Value:"99.6"},{Indicator:"Population Urban (% of population)",Country:"Japan",Year:"2013",Value:"97.6"},{Indicator:"Population Urban (% of population)",Country:"China",Year:"2012",Value:"95.6"},{Indicator:"Population Urban (% of population)",Country:"China",Year:"2013",Value:"99.6"}];
     
    const merged = array.reduce((acc, { Indicator, Country, Year, Value }) => {
        const key = `${Country}-${Indicator}`
        acc[key] = acc[key] || { Country, Indicator }
        acc[key][Year] = Value;
        return acc
    }, {})
    
    const output = Object.values(merged);
    console.log(output)

    这个想法是创建一个累加器对象,其键等于CountryIndicator 的每个唯一组合,如下所示:

    {
      "India-Population National (% of population)": {
        "2012": "98.8",
        "2013": "99.1",
        "Country": "India",
        "Indicator": "Population National (% of population)"
      },
      "India-Population Urban (% of population)": {..}
      ...
    }
    

    然后使用Object.values 获取数组中的这些值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 2015-11-04
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多