我正在使用ChartKick来表示多个系列折线图中的一些数据。所需格式为:
let data = [
{ name: 'sharry', data: { '2008-08-02': 10006097, '2018-08-03': 10670, etc }},
{ name: 'stuart', data: { '2018-08-01' :1401231321, '2018-08-04': 2500000000, etc }}
];
我从数据库中获得的数据是:
let user_usage = [
{ "name":"stuart", "data": { "2018-08-02":10006097 } },
{ "name":"stuart", "data": { "2018-08-03":10670 } },
{ "name":"stuart", "data": { "2018-08-05":5800000 } },
{ "name":"stuart", "data": { "2018-08-07":10789000 } },
{ "name":"stuart", "data": { "2018-08-08":1033000000 } },
{ "name":"sharry", "data": { "2018-08-01":1401231321 } },
{ "name":"sharry", "data": { "2018-08-04":2500000000 } }
];
我已经尝试了2天,以便将数据库响应转换为上面要求的格式。任何帮助,谢谢大家。这是我到目前为止所拥有的。我正在使用Ramda,但可以使用ES6:
let chartdata = [];
let u = R.groupBy(R.prop('name'))(user_usage);
Object.keys(u).forEach((name, index) => {
chartdata.push({ name , data: {} });
let usage = u[name];
Object.keys(usage).forEach(row => {
let data = usage[row].data;
let date = Object.keys(data)[0];
let total = data[Object.keys(data)[0]];
chartdata[index].data = Object.assign({}, {`${date}: ${total}`};
});
});
答案 0 :(得分:2)
将Array.reduce()
与Object.assign()
一起使用,以便可以将data
对象分配给累加器的现有项目。
let user_usage = [
{ "name":"stuart", "data": { "2018-08-02":10006097 } },
{ "name":"stuart", "data": { "2018-08-03":10670 } },
{ "name":"stuart", "data": { "2018-08-05":5800000 } },
{ "name":"stuart", "data": { "2018-08-07":10789000 } },
{ "name":"stuart", "data": { "2018-08-08":1033000000 } },
{ "name":"sharry", "data": { "2018-08-01":1401231321 } },
{ "name":"sharry", "data": { "2018-08-04":2500000000 } }
];
var res = user_usage.reduce((acc, item) => {
var existItem = acc.find(({name}) => name === item.name);
if(existItem){
Object.assign(existItem.data,item.data);
} else {
acc.push(item);
}
return acc;
},
[]);
console.log(res);
答案 1 :(得分:1)
var obj = {};
user_usage.forEach(function(e){
if(!obj[e.name]){
obj[e.name] = e.data;
} else {
obj[e.name] = Object.assign(obj[e.name], e.data)
}
});
var array = Object.keys(obj).map(function(e){return {name: e, data: obj[e]}})
答案 2 :(得分:0)
您可以执行以下操作:
let user_usage = [
{ "name":"stuart", "data": { "2018-08-02":10006097 } },
{ "name":"stuart", "data": { "2018-08-03":10670 } },
{ "name":"stuart", "data": { "2018-08-05":5800000 } },
{ "name":"stuart", "data": { "2018-08-07":10789000 } },
{ "name":"stuart", "data": { "2018-08-08":1033000000 } },
{ "name":"sharry", "data": { "2018-08-01":1401231321 } },
{ "name":"sharry", "data": { "2018-08-04":2500000000 } }
];
let data = user_usage.reduce((a, c) => {
let key = Object.keys(c.data)[0];
let found = a.find(v => v.name === c.name);
if(found){
found.data[key] = c.data[key];
}else{
a.push(c);
}
return a;
}, [])
console.log(data);
答案 3 :(得分:0)
您可以使用Object.assign()
和Map()
获得更有效的解决方案:
let user_usage = [ { "name":"stuart", "data": { "2018-08-02":10006097 } }, { "name":"stuart", "data": { "2018-08-03":10670 } }, { "name":"stuart", "data": { "2018-08-05":5800000 } }, { "name":"stuart", "data": { "2018-08-07":10789000 } }, { "name":"stuart", "data": { "2018-08-08":1033000000 } }, { "name":"sharry", "data": { "2018-08-01":1401231321 } }, { "name":"sharry", "data": { "2018-08-04":2500000000 } } ];
var map = new Map();
user_usage.forEach((obj)=>{
if(map.has(obj.name)){
var mapObject = map.get(obj.name);
Object.assign(mapObject.data, obj.data);
map.set(obj.name ,mapObject);
} else{
map.set(obj.name, Object.assign({},obj));
}
});
let result = Array.from(map.values());
console.log(result);
答案 4 :(得分:0)
这也是一个ramda示例供参考:
pipe(
groupBy(prop('name')),
map(pipe(pluck('data'), mergeAll)),
)