2017-02-27 76 views
-2

我有一个数组,我需要能够从它得到2个数组。我需要这两个数组作为图表。图表组件需要一个标签数组和一个总数组(每个标签)。从一个数组创建2个数组ecmascript 2015

我还在学习新的数组方法,的forEach,地图等于是用我裸,这是数组我有:

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。

+0

'const的量= income.map(值=> value.amount)' –

+0

运算 '按组' 求和不量的列表需要一个。 – rasmeister

+1

'forEach'和'map'不是“新”。他们老了”。它们也不是ECMAScript 2015,也就是ES6。他们是ES5。 – 2017-02-28 03:29:24

回答

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

编辑的评论

要添加到您使用set()映射中的项 - 在上面的例子中,我使用的说明财产作为密钥,并且添加金额。在这部分代码:

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

基本上只是检查,如果创建的地图拥有像迭代描述的关键,如果是的话,它需要在现有量补充。 ||短路,所以如果没有现有量(a.get(b.description)未定义),则取而代之。英语不是我的第一语言 - 这里是什么,我的意思是一个简单的例子:

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

+2

我喜欢它。仍然需要了解'Map'是如何工作的:/ –

+2

Map就像一个Object,除了它可以有任何对象(不仅仅是字符串)作为关键字,并且你可以使用'get'和'set'来获得,呃得到并设置成员。 –

+3

不是唯一的区别不过,读[这里](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Objects_and_maps_compared)@Kinduser – baao

0

另一种方法:

  • 滤波器income可变摆脱重复说明
  • 获取已过滤数组的每个元素并遍历income数组以获取具有相同的条目值然后总结它们的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);

+0

我不能运行自动柜员机,但如果我理解正确,说明是关键,金额是价值? –

+0

@AnnaSmother正确。 –