我需要将unflattened数据转换为正确的格式。 有人可以帮我搞定吗? 这是一个JSFiddle: http://jsfiddle.net/kawells/vse7fbn3/4/
var arr = [{"id":"33155","comment":"Program 02 - Student Care - Library training","isfreetext":"0","snapshotfactorid":"1","ssprogramname":"Student Care","ssprogramnumber":"2","ssfactorname":"","ssfactornumber":"","isselected":false,"reportcommentid":"13738"},
{"id":"33154","comment":"Program 02 - Student Care - Library book acquired ","isfreetext":"0","snapshotfactorid":"1","ssprogramname":"Student Care","ssprogramnumber":"2","ssfactorname":"","ssfactornumber":"","isselected":false,"reportcommentid":"13737"},
{"id":"33656","comment":"Program 09 - Communications Check Communications Check Student addressed","isfreetext":"0","snapshotfactorid":"2","ssprogramname":"Communication","ssprogramnumber":"9","ssfactorname":"","ssfactornumber":"","update_id":"1415294556708","isselected":false,"reportcommentid":"14234"},
{"id":"33658","comment":"Program 09 Communications Check - counselling conducted weekly for Student ","isfreetext":"0","snapshotfactorid":"2","ssprogramname":"Communication","ssprogramnumber":"9","ssfactorname":"","ssfactornumber":"","isselected":false,"reportcommentid":"14236"}]
grouped = _.map(_.groupBy(arr, 'ssprogramnumber'), function(b) {
return _.extend(_.pick(b[0], 'ssprogramnumber', 'ssprogramname'), {
factors: _.map(b, function(elem) {
return _.pick(elem, 'snapshotfactorid', 'comment')
})
});
});
document.body.innerHTML = "<pre>" + (JSON.stringify(grouped, null, " "))
应该看起来像:
[
{
"ssprogramnumber": "2",
"ssprogramname": "Student Care",
"factors": [
{
"snapshotfactorid": "1",
comments:[{id:33155,"comment": "Program 02 - Student Care - Library training"},
{id:33154,"comment": "Program 02 - Student Care - Library book acquired "}]
}
]
},
{
"ssprogramnumber": "9",
"ssprogramname": "Communication",
"factors": [
{
"snapshotfactorid": "2",
comments:[{id: 33656 ,"comment": "Program 09 - Communications Check Communications Check Student addressed"},
{id:33658,"comment": "Program 09 Communications Check - counselling conducted weekly for Student "}]
}
]
}
]
---回答第二个问题 - 答案 - 对于那些可能需要了解的人,请参阅下文。
var programgroups = _.groupBy(arr,&#39; ssprogramnumber&#39;);
var result = _.map(programgroups,function(program){
var snapshotgroups = _.groupBy(program, 'snapshotfactorid');
return {
ssprogramnumber: program[0].ssprogramnumber,
ssprogramname: program[0].ssprogramname,
factors: _.map(snapshotgroups, function(snapshotfactor){
return {
snapshotfactorid: snapshotfactor[0].snapshotfactorid,
ssfactornumber: snapshotfactor[0].ssfactornumber,
ssfactorname: snapshotfactor[0].ssfactorname,
comments: _.map(snapshotfactor, function(comment){
return _.pick(comment, 'id', 'comment');
})
}
})
}
});
答案 0 :(得分:2)
这应该做你想要的:
var programgroups = _.groupBy(arr, 'ssprogramnumber');
var result = _.map(programgroups, function(program){
var snapshotgroups = _.groupBy(program, 'snapshotfactorid');
return {
ssprogramnumber: program[0].ssprogramnumber,
ssprogramname: program[0].ssprogramname,
factors: _.map(snapshotgroups, function(snapshotgroup, snapshotfactorid){
return {
snapshotfactorid: snapshotfactorid,
comments: _.map(snapshotgroup, function(comment){
return _.pick(comment, 'id', 'comment');
})
}
})
}
});