我需要重组这个json文件:
[
{
"Id":1,
"userId":"dd1789f1-bb28-4f2c-975a-bad0e3d127cd",
"email":"[email protected]",
"projName":"1",
"taskName":"2",
"startDate":"2017-01-22",
"endDate":"2017-01-28",
"hr_Sun":"0.00",
"hr_Mon":"0.00",
"hr_Tue":"0.00",
"hr_Wed":"0.00",
"hr_Thu":"0.00",
"hr_Fri":"0.00",
"hr_Sat":"0.00",
"total_hr":"0.00"
},
{
"Id":2,
"userId":"dd1789f1-bb28-4f2c-975a-bad0e3d127cd",
"email":"[email protected]",
"projName":"1",
"taskName":"3",
"startDate":"2017-01-22",
"endDate":"2017-01-28",
"hr_Sun":"0.00",
"hr_Mon":"0.00",
"hr_Tue":"0.00",
"hr_Wed":"0.00",
"hr_Thu":"0.00",
"hr_Fri":"0.00",
"hr_Sat":"0.00",
"total_hr":"0.00"
},
{
"Id":3,
"userId":"dd1789f1-bb28-4f2c-975a-bad0e3d127cd",
"email":"[email protected]",
"projName":"2",
"taskName":"3",
"startDate":"2017-01-22",
"endDate":"2017-01-28",
"hr_Sun":"0.00",
"hr_Mon":"0.00",
"hr_Tue":"0.00",
"hr_Wed":"0.00",
"hr_Thu":"0.00",
"hr_Fri":"0.00",
"hr_Sat":"0.00",
"total_hr":"0.00"
}
]
我的最终结果应该是这样的:
{
"projects":[
{
"projName":"1",
"task_data":[
{
"taskName":"2",
"task_detail_data":[
{
"h_sun":"0.00",
"h_mon":"0.00",
"h_tue":"0.00",
"h_wed":"0.00",
"h_thu":"0.00",
"h_fri":"0.00",
"h_sat":"0.00"
}
]
},
{
"taskName":"3",
"task_detail_data":[
{
"h_sun":"0.00",
"h_mon":"0.00",
"h_tue":"0.00",
"h_wed":"0.00",
"h_thu":"0.00",
"h_fri":"0.00",
"h_sat":"0.00"
}
]
}
]
},
{
"projName":"2",
"task_data":[
{
"taskName":"3",
"task_detail_data":[
{
"h_sun":"0.00",
"h_mon":"0.00",
"h_tue":"0.00",
"h_wed":"0.00",
"h_thu":"0.00",
"h_fri":"0.00",
"h_sat":"0.00"
}
]
}
]
}
因此,具有相同项目的任务将保留在相同的项目数组中。但是,在我当前的代码中,即使ID相同,它也会继续返回不同的数组。
var project_data = [];
var proName = "";
for (var i = 0; i < arr.length; i++)
{
var startDate = arr[i].startDate;
var endDate = arr[i].endDate;
var week_data = { startDate, endDate };
var projName = arr[i].projName;
if (proName != projName) {
//create new task
proName = arr[i].projName;
// alert(projName);
var taskName = arr[i].taskName;
var task_data = [];
var task_detail_data = [];
var h_sun = arr[i].hr_Sun;
var h_mon = arr[i].hr_Mon;
var h_tue = arr[i].hr_Tue;
var h_wed = arr[i].hr_Wed;
var h_thu = arr[i].hr_Thu;
var h_fri = arr[i].hr_Fri;
var h_sat = arr[i].hr_Sat;
task_detail_data.push({ h_sun, h_mon, h_tue, h_wed, h_thu, h_fri, h_sat });
task_data.push({ taskName, task_detail_data });
}
else {
var taskName = arr[i].taskName;
var task_data = [];
var task_detail_data = [];
var h_sun = arr[i].hr_Sun;
var h_mon = arr[i].hr_Mon;
var h_tue = arr[i].hr_Tue;
var h_wed = arr[i].hr_Wed;
var h_thu = arr[i].hr_Thu;
var h_fri = arr[i].hr_Fri;
var h_sat = arr[i].hr_Sat;
task_detail_data.push({ h_sun, h_mon, h_tue, h_wed, h_thu, h_fri, h_sat });
task_data.push({ taskName, task_detail_data });
}
project_data.push({ projName, task_data });
}
var output = { projects: project_data };
var json = JSON.stringify(output);
$('#output').html(json);
}
伙计们,请帮我解决这个问题。非常感谢
最佳答案
试试以下代码片段:
var arr = [
{
"Id": 1,
"userId": "dd1789f1-bb28-4f2c-975a-bad0e3d127cd",
"email": "[email protected]",
"projName": "1",
"taskName": "2",
"startDate": "2017-01-22",
"endDate": "2017-01-28",
"hr_Sun": "0.00",
"hr_Mon": "0.00",
"hr_Tue": "0.00",
"hr_Wed": "0.00",
"hr_Thu": "0.00",
"hr_Fri": "0.00",
"hr_Sat": "0.00",
"total_hr": "0.00"
},
{
"Id": 2,
"userId": "dd1789f1-bb28-4f2c-975a-bad0e3d127cd",
"email": "[email protected]",
"projName": "1",
"taskName": "3",
"startDate": "2017-01-22",
"endDate": "2017-01-28",
"hr_Sun": "0.00",
"hr_Mon": "0.00",
"hr_Tue": "0.00",
"hr_Wed": "0.00",
"hr_Thu": "0.00",
"hr_Fri": "0.00",
"hr_Sat": "0.00",
"total_hr": "0.00"
},
{
"Id": 3,
"userId": "dd1789f1-bb28-4f2c-975a-bad0e3d127cd",
"email": "[email protected]",
"projName": "2",
"taskName": "3",
"startDate": "2017-01-22",
"endDate": "2017-01-28",
"hr_Sun": "0.00",
"hr_Mon": "0.00",
"hr_Tue": "0.00",
"hr_Wed": "0.00",
"hr_Thu": "0.00",
"hr_Fri": "0.00",
"hr_Sat": "0.00",
"total_hr": "0.00"
}
];
var project_data = [];
var proName = "";
for (var i = 0; i < arr.length; i++) {
var startDate = arr[i].startDate;
var endDate = arr[i].endDate;
var week_data = { "startDate": startDate, "endDate": endDate };
var projName = arr[i].projName;
if (proName != projName) {
//create new task
proName = arr[i].projName;
// alert(projName);
var taskName = arr[i].taskName;
var task_data = [];
var task_detail_data = [];
var h_sun = arr[i].hr_Sun;
var h_mon = arr[i].hr_Mon;
var h_tue = arr[i].hr_Tue;
var h_wed = arr[i].hr_Wed;
var h_thu = arr[i].hr_Thu;
var h_fri = arr[i].hr_Fri;
var h_sat = arr[i].hr_Sat;
task_detail_data.push({ "h_sun": h_sun, "h_mon": h_mon, "h_tue": h_tue, "h_wed": h_wed, "h_thu": h_thu, "h_fri": h_fri, "h_sat": h_sat });
task_data.push({ "taskName": taskName, "task_detail_data": task_detail_data });
project_data.push({ "projName": projName, "task_data": task_data });
}
else {
var taskName = arr[i].taskName;
var task_data = [];
var task_detail_data = [];
var h_sun = arr[i].hr_Sun;
var h_mon = arr[i].hr_Mon;
var h_tue = arr[i].hr_Tue;
var h_wed = arr[i].hr_Wed;
var h_thu = arr[i].hr_Thu;
var h_fri = arr[i].hr_Fri;
var h_sat = arr[i].hr_Sat;
task_detail_data.push({ "h_sun": h_sun, "h_mon": h_mon, "h_tue": h_tue, "h_wed": h_wed, "h_thu": h_thu, "h_fri": h_fri, "h_sat": h_sat });
task_data.push({ "taskName": taskName, "task_detail_data": task_detail_data });
var index = project_data.map(function (o) { return o.projName; }).indexOf("1");
project_data[index].task_data.push(task_data);
}
}
var output = { projects: project_data };
var json = JSON.stringify(output);
$('#output').html(json);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>