从一个数组ecmascript 2015创建2个数组

时间:2017-02-27 23:39:56

标签: javascript arrays ecmascript-6

我有一个数组,我需要能够从中获得2个数组。我需要这两个数组用于图表。图表组件需要标签数组和总计数组(对于每个标签)。

这是我拥有的数组:

const income =  [
    {
      "date": 1482233886000,
      "name": "Company A",
      "type": "Salary",
      "description": "Salary jan. 2017",
      "amount": 50.6
    },
    {
      "date": 1482406686000,
      "name": "Company B",
      "type": "Salary",
      "description": "Salary jan. 2017",
      "amount": 40.23
    },
    {
      "date": 1485171486000,
      "name": "Company A",
      "type": "Salary",
      "description": "Salary feb. 2017",
      "amount": 200.2
    },
    {
      "date": 1485344286000,
      "name": "Company B",
      "type": "Salary",
      "description": "Salary feb. 2017",
      "amount": 100.4
    },
    {
      "date": 1490655892000,
      "name": "Company A",
      "type": "Salary",
      "description": "Salary mar. 2017",
      "amount": 20
    },
    {
      "date": 1493335850000,
      "name": "Company A",
      "type": "Salary",
      "description": "Salary apr. 2017",
      "amount": 15
    },
    {
      "date": 1493335850000,
      "name": "Company B",
      "type": "Salary",
      "description": "Salary apr. 2017",
      "amount": 10
    }
  ];

要获取标签,我这样做是为了获得唯一标签(描述):

[...new Set(income.map(value => value.description))]
=> ["Salary jan. 2017", "Salary feb. 2017", "Salary mar. 2017", "Salary apr. 2017"]

这有效,但现在我想要每个描述的总量,例如:

//[90.83, 300.6, 20, 25] 

如果有可能的话,有人可以帮助我使用最后一个数组和ES6风格。我想与减少一起去,但我无法与之相提并论。所以我希望大师可以教我; D。

2 个答案:

答案 0 :(得分:3)

您可以缩小为Map,生成的keys()和values()将是您的数组,按描述进行分组和求和。

const income = [{"date": 1482233886000,"name": "Company A","type": "Salary","description": "Salary jan. 2017","amount": 50.6},{"date": 1482406686000,"name": "Company B","type": "Salary","description": "Salary jan. 2017","amount": 40.23},{"date": 1485171486000,"name": "Company A","type": "Salary","description": "Salary feb. 2017","amount": 200.2},{"date": 1485344286000,"name": "Company B","type": "Salary","description": "Salary feb. 2017","amount": 100.4},{"date": 1490655892000,"name": "Company A","type": "Salary","description": "Salary mar. 2017","amount": 20},{"date": 1493335850000,"name": "Company A","type": "Salary","description": "Salary apr. 2017","amount": 15},{"date": 1493335850000,"name": "Company B","type": "Salary","description": "Salary apr. 2017","amount": 10}];

let res = income.reduce((a, b) => {
  a.set(b.description, (a.get(b.description) || 0) + b.amount);
  return a;
}, new Map());

console.log([...res.values()]); // the summed amount
console.log([...res.keys()]); // the description property

编辑您的评论

要向Map添加条目,请使用set() - 在上面的示例中,我使用description属性作为键并将数量添加到该键。这部分代码:

(a.get(b.description) || 0)

基本上只是检查创建的Map是否包含迭代描述之类的键,如果是,则需要将现有数量添加到其中。 ||短路,因此如果没有现有数量(a.get(b.description)未定义),则取代0。英语不是我的第一语言 - 这是我的意思的一个更简单的例子:

let m = new Map();
m.set("foo", 1);
let a = m.get("foo");
console.log(a); // 1 - obvious
let b = m.get("bar");
console.log(b); // undefined

console.log(b || 0); // 0, as b is undefined, 0 is taken
// in the original answer, this would be the same as 
if (! b) b = 0;
console.log(b); 0

let c = (b || 0) + 1; // 0 + 1 because b is undefined;
console.log("c: ", c);

let d = (a || 0) + 1; // 1 + 1 because a holds 1;
console.log("d:", d);

答案 1 :(得分:0)

另一种方法:

  • 过滤income变量以删除重复说明
  • 获取已过滤数组的每个元素并迭代income数组以获取具有相同description值的条目,然后汇总其amount属性
  • 将每个元素的总量推入新制作的arr数组。

const income=[{date:1482233886e3,name:"Company A",type:"Salary",description:"Salary jan. 2017",amount:50.6},{date:1482406686e3,name:"Company B",type:"Salary",description:"Salary jan. 2017",amount:40.23},{date:1485171486e3,name:"Company A",type:"Salary",description:"Salary feb. 2017",amount:200.2},{date:1485344286e3,name:"Company B",type:"Salary",description:"Salary feb. 2017",amount:100.4},{date:1490655892e3,name:"Company A",type:"Salary",description:"Salary mar. 2017",amount:20},{date:149333585e4,name:"Company A",type:"Salary",description:"Salary apr. 2017",amount:15},{date:149333585e4,name:"Company B",type:"Salary",description:"Salary apr. 2017",amount:10}];

var arr = [];
var sorted = [...new Set(income.map(value => value.description))];
sorted.forEach(function(v){
  var obj = {};
  var sum = 0;
  income.forEach(function(c){
    if (c.description == v){
      sum += c.amount;
    }
  });
  obj[v] = sum;
  arr.push(obj);
});

console.log(arr);