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.
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.