我有这种格式的JSON。 我无法弄清楚这个问题 我使用Object.values和object.keys与Array.prototype.map(),但我的算法没有做任何我想做的事。
[
{
"2018-01-01": [
{
"firstname": "mati",
"lastname": "mati",
"userid": null,
"total_minutes": 0,
"free_minutes": 480,
"task_date": "2018-01-01"
},
{
"firstname": "andrzej",
"lastname": "owsianka",
"userid": null,
"total_minutes": 0,
"free_minutes": 480,
"task_date": "2018-01-01"
},
{
"firstname": "asd",
"lastname": "asd",
"userid": null,
"total_minutes": 0,
"free_minutes": 480,
"task_date": "2018-01-01"
}
],
"2018-01-02": [
{
"firstname": "mati",
"lastname": "mati",
"userid": null,
"total_minutes": "70",
"task_date": "2018-01-02",
"free_minutes": 410
},
{
"firstname": "andrzej",
"lastname": "owsianka",
"userid": null,
"total_minutes": "360",
"task_date": "2018-01-02",
"free_minutes": 120
},
{
"firstname": "asd",
"lastname": "asd",
"userid": null,
"total_minutes": "30",
"task_date": "2018-01-02",
"free_minutes": 450
}
],
"2018-01-03": [
{
"firstname": "mati",
"lastname": "mati",
"userid": null,
"total_minutes": "0",
"task_date": "2018-01-03",
"free_minutes": 480
},
{
"firstname": "andrzej",
"lastname": "owsianka",
"userid": null,
"total_minutes": 0,
"free_minutes": 480,
"task_date": "2018-01-03"
},
{
"firstname": "asd",
"lastname": "asd",
"userid": null,
"total_minutes": 0,
"free_minutes": 480,
"task_date": "2018-01-03"
}
]
}
]
我希望以这种格式获取JSON或与此格式非常相似
[
{
"firstname": "mati",
"lastname": "mati",
"2018-01-01": "480",
"2018-01-02": 480,
"2018-01-03": 480
},
{
"firstname": "andrzej",
"lastname": "owsianka",
"2018-01-01": "480",
"2018-01-02": 480,
"2018-01-03": 480
},
{
"firstname": "asd",
"lastname": "asd",
"2018-01-01": "480",
"2018-01-02": 480,
"2018-01-03": 480
}
]
我如何映射数据以获得类似的东西? 我大约2小时打架但是,我没有发明任何意义。
答案 0 :(得分:0)
你应该在一个普通的结果累加器中迭代并累积所有嵌套的部分,这是伪代码:
var result=[];
for each dates in json
for each date in dates
for each entry in date
result[]=entry;
答案 1 :(得分:0)
您需要使用reduce
和forEach
,并根据firstname
和lastname
var output = Object.values( arr.reduce( function(a, c){
Object.keys( c ).forEach( function( date ){
var arr2 = c[ date ];
arr2.forEach( function(item){
var keyObj = {firstname:item.firstname, lastname:item.lastname};
var key = JSON.stringify( keyObj );
a[ key ] = a[ key ] || keyObj;
a[ key ][date] = item.free_minutes;
});
});
return a;
} ,{}));
<强>演示强>
var arr = [{
"2018-01-01": [{
"firstname": "mati",
"lastname": "mati",
"userid": null,
"total_minutes": 0,
"free_minutes": 480,
"task_date": "2018-01-01"
},
{
"firstname": "andrzej",
"lastname": "owsianka",
"userid": null,
"total_minutes": 0,
"free_minutes": 480,
"task_date": "2018-01-01"
},
{
"firstname": "asd",
"lastname": "asd",
"userid": null,
"total_minutes": 0,
"free_minutes": 480,
"task_date": "2018-01-01"
}
],
"2018-01-02": [{
"firstname": "mati",
"lastname": "mati",
"userid": null,
"total_minutes": "70",
"task_date": "2018-01-02",
"free_minutes": 410
},
{
"firstname": "andrzej",
"lastname": "owsianka",
"userid": null,
"total_minutes": "360",
"task_date": "2018-01-02",
"free_minutes": 120
},
{
"firstname": "asd",
"lastname": "asd",
"userid": null,
"total_minutes": "30",
"task_date": "2018-01-02",
"free_minutes": 450
}
],
"2018-01-03": [{
"firstname": "mati",
"lastname": "mati",
"userid": null,
"total_minutes": "0",
"task_date": "2018-01-03",
"free_minutes": 480
},
{
"firstname": "andrzej",
"lastname": "owsianka",
"userid": null,
"total_minutes": 0,
"free_minutes": 480,
"task_date": "2018-01-03"
},
{
"firstname": "asd",
"lastname": "asd",
"userid": null,
"total_minutes": 0,
"free_minutes": 480,
"task_date": "2018-01-03"
}
]
}];
var output = Object.values( arr.reduce( function(a, c){
Object.keys( c ).forEach( function( date ){
var arr2 = c[ date ];
arr2.forEach( function(item){
var keyObj = {firstname:item.firstname, lastname:item.lastname};
var key = JSON.stringify( keyObj );
a[ key ] = a[ key ] || keyObj;
a[ key ][date] = item.free_minutes;
});
});
return a;
} ,{}));
console.log( output );