我正在尝试根据时间戳减少此数组:
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}]
我曾尝试使用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))
我希望的结果是:
Var New_Data = [{"Date":"xx/xx/xx", "Assets":[{"group":"XXX"},{"Count":"X"}]},{"Date":"xx/xx/xx","Assets":[{"group":"XXX"},{"Count":"X"}]}]
任何想法如何实现这一点,任何帮助都会很棒。 谢谢=)
答案 0 :(得分:2)
以下代码是一个工作示例。如果它们与我实施的不同,您应该能够将其改为您的确切规格。
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);

您可能需要将生成的对象转换为数组:
new_data = [];
for(i in Data){
new_data.push(Data[i]);
}
答案 1 :(得分:0)
你需要的是这样的东西吗?
var result = dados.map(function (v) {
return {
Date: new Date(v["Date"]),
Assets: [
{Group: v['Group']},
{Count: v['Count']}
]
};
});
或者你需要创建一个小组吗?