[英]JavaScript Reduce array on timestamp
I am trying to reduce this array based on the timestamp: 我试图根据时间戳减少此数组:
Var Data = [{"Group":"OPS","Date":"2017-04-18T00:00:00.000Z","Count":1},{"Group":"BOE","Date":"2017-04-19T00:00:00.000Z","Count":1},{"Group":"EDW","Date":"2017-04-21T00:00:00.000Z","Count":1},{"Group":"SUPPORT","Date":"2017-04-17T00:00:00.000Z","Count":1},{"Group":"EDWEXTRACTS","Date":"2017-04-17T00:00:00.000Z","Count":1},{"Group":"EDWEXTRACTS","Date":"2017-04-18T00:00:00.000Z","Count":1},{"Group":"SAFTT","Date":"2017-04-17T00:00:00.000Z","Count":1},{"Group":"SAFTT","Date":"2017-04-18T00:00:00.000Z","Count":2},{"Group":"SAFTT","Date":"2017-04-21T00:00:00.000Z","Count":1},{"Group":"OPS","Date":"2017-04-17T00:00:00.000Z","Count":1},{"Group":"VIEW","Date":"2017-04-17T00:00:00.000Z","Count":1},{"Group":"VIEW","Date":"2017-04-19T00:00:00.000Z","Count":3}]
I have tried to use the reduce method but seem to be doing something wrong. 我尝试使用reduce方法,但似乎做错了什么。
var obj = {}
result.reduce(function(r, e) {
if (!obj[e.Date]) {
obj[e.Date] = {
Date: e.Date,
Assest: []
}
r.push(obj[e.Date])
}
obj[e.Date].Date.push(e.Group)
obj[e.Date].Date.push(e.Count)
return r
}, [])
console.log(JSON.stringify(result, 0, 4))
My desired outcome would be: 我期望的结果是:
Var New_Data = [{"Date":"xx/xx/xx", "Assets":[{"group":"XXX"},{"Count":"X"}]},{"Date":"xx/xx/xx","Assets":[{"group":"XXX"},{"Count":"X"}]}]
Any Idea how I can achieve this, any help would be great. 任何想法,我将如何实现这一目标,任何帮助都会很棒。 Thanks =)
谢谢=)
The code below is a working example. 下面的代码是一个有效的示例。 With this in hand you should be able to alter it to your exact specifications, if they differ from what I have implemented.
如果这与我实现的内容不同,那么您应该可以将其更改为确切的规格。
var Data = [ {"Group":"OPS","Date":"2017-04-18T00:00:00.000Z","Count":1}, {"Group":"BOE","Date":"2017-04-19T00:00:00.000Z","Count":1}, {"Group":"EDW","Date":"2017-04-21T00:00:00.000Z","Count":1}, {"Group":"SUPPORT","Date":"2017-04-17T00:00:00.000Z","Count":1}, {"Group":"EDWEXTRACTS","Date":"2017-04-17T00:00:00.000Z","Count":1}, {"Group":"EDWEXTRACTS","Date":"2017-04-18T00:00:00.000Z","Count":1}, {"Group":"SAFTT","Date":"2017-04-17T00:00:00.000Z","Count":1}, {"Group":"SAFTT","Date":"2017-04-18T00:00:00.000Z","Count":2}, {"Group":"SAFTT","Date":"2017-04-21T00:00:00.000Z","Count":1}, {"Group":"OPS","Date":"2017-04-17T00:00:00.000Z","Count":1}, {"Group":"VIEW","Date":"2017-04-17T00:00:00.000Z","Count":1}, {"Group":"VIEW","Date":"2017-04-19T00:00:00.000Z","Count":3} ]; Data = Data.reduce(function(r, e) { var date = e.Date.match(/(\\d+-\\d+-\\d+)/)[0].replace(/-/g, '/'); if (r[date] == undefined) { r[date] = {Date: date, Assets: []}; } r[date].Assets.push({Group: e.Group}); r[date].Assets.push({Count: e.Count}); return r }, {}); console.log(Data);
You may need to convert the resulting object into an array: 您可能需要将结果对象转换为数组:
new_data = [];
for(i in Data){
new_data.push(Data[i]);
}
What you need is something like this? 您需要的是这样的东西吗?
var result = dados.map(function (v) {
return {
Date: new Date(v["Date"]),
Assets: [
{Group: v['Group']},
{Count: v['Count']}
]
};
});
or Do you need to create a group? 还是您需要创建一个组?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.