使用javascript将对象数组转换为一组新的Object?

时间:2017-03-17 13:41:18

标签: javascript arrays json javascript-objects

这个问题已经被提出了许多不同的形式,但我还没有能够将它们拼凑起来以实现我正在寻找的结果。所以,抱歉重复重复...

我想要做的就是采用这样的数组:

[
  0: { 
       2014: "6", 
       2015: "19",
       2016: "3",
       2017: "12",
       Name: "Jerry"
     },
  1: { 
       2014: "16", 
       2015: "9",
       2016: "",
       2017: "16",
       Name: "Bill"
     },
  2: { 
       2014: "", 
       2015: "2",
       2016: "43",
       2017: "7",
       Name: "Grace"
     }
]

这样的事情:

{
    "years": {
        "2014": {
            "Jerry": 6,
            "Bill": 16,
            "Grace": ""
        },
        "2015": {
            "Jerry": 19,
            "Bill": 9,
            "Grace": 2
        },
        "2016": {
            "Jerry": 3,
            "Bill": "",
            "Grace": 43
        },
        "2017": {
            "Jerry": 12,
            "Bill": 16,
            "Grace": 7
        }
    }
}

在尝试填充,删除,重复,重复......之后,我还有一些代码:

仅供参考 - 数据来自从D3.js csv()

加载的CSV文件
let loadData = () => {
    return d3.csv(dataFile, function cb (err, d) {
        if (err) throw err;
        return d;
    });
};

loadData().get((err, n) => {
    if (err) throw err;

    let years = [];

    years.push(Object.keys(n[0]));

    years = _.flatten(years);
    years.splice(-1, 1);


    console.log(n[0]);

    n.map((x) => {
        console.log(Object.keys(x[0]));
    });

});

5 个答案:

答案 0 :(得分:2)

您可以使用“reduce”来实现此目的。

var data = [{
  2014: "6",
  2015: "19",
  2016: "3",
  2017: "12",
  Name: "Jerry"
}, {
  2014: "16",
  2015: "9",
  2016: "",
  2017: "16",
  Name: "Bill"
}, {
  2014: "",
  2015: "2",
  2016: "43",
  2017: "7",
  Name: "Grace"
}];

data = data.reduce(function(pre, v) {

  for (var i in v) {
    if (i === 'Name') {
      continue;
    }
    if (!pre[i]) {
      pre[i] = {};
    };

    pre[i][v.Name] = v[i];

  }
  return pre;
}, {});
data = {
  years: data
};
console.log(data);

答案 1 :(得分:1)

您可以迭代数组和键并构建一个新对象。

var data = [{ 2014: "6", 2015: "19", 2016: "3", 2017: "12", Name: "Jerry" }, { 2014: "16", 2015: "9", 2016: "", 2017: "16", Name: "Bill" }, { 2014: "", 2015: "2", 2016: "43", 2017: "7", Name: "Grace" }],
    grouped = { years: {} };

data.forEach(function (o) {
    Object.keys(o).forEach(function (k) {
        if (k !== 'Name') {
            grouped.years[k] = grouped.years[k] || {};
            grouped.years[k][o.Name] = o[k];
        }
    });
});

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

您可以使用reduce()构建对象,在内部,您可以使用Object.keys()forEach()循环为每年添加对象。



var arr = [{"2014":"6","2015":"19","2016":"3","2017":"12","Name":"Jerry"},{"2014":"16","2015":"9","2016":"","2017":"16","Name":"Bill"},{"2014":"","2015":"2","2016":"43","2017":"7","Name":"Grace"}]

var result = arr.reduce(function(r, o) {
  Object.keys(o).forEach(function(e) {
    if(!r.years[e]  && e != 'Name') r.years[e] = {}
    Object.assign(r.years[e] || {}, {[o.Name]: o[e]})
  })
  return r;
}, {years: {}})

console.log(JSON.stringify(result, 0, 4))




答案 3 :(得分:0)

您可以在此方案中使用Array.prototype.reduce方法。此方法的第一个参数是reducer,第二个参数是初始值。



var input = [{
    2014: "6",
    2015: "19",
    2016: "3",
    2017: "12",
    Name: "Jerry"
  },
  {
    2014: "16",
    2015: "9",
    2016: "",
    2017: "16",
    Name: "Bill"
  },
  {
    2014: "",
    2015: "2",
    2016: "43",
    2017: "7",
    Name: "Grace"
  }
];

var output = input.reduce((a, b) => {
  for (var k in b) {
    if (k !== 'Name') {
      a.year[k] = a.year[k] || {};
      a.year[k][b.Name] = b[k];
    }
  }
  return a;
}, {
  years: {}
});

console.log(output);




答案 4 :(得分:0)

reduce不是强制性的。例如:

let result = { years : {} };
for (let record of input) {
    for (let key in record) {
        if (key == 'Name') continue;
        result.years[key] = result.years[key] || {};
        result.years[key][record.Name] = record[key];
    }
}