我需要重构这个json文件:
[
{
"Id":1,
"userId":"dd1789f1-bb28-4f2c-975a-bad0e3d127cd",
"email":"lalaland@gmail.com",
"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":"lalaland@gmail.com",
"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":"lalaland@gmail.com",
"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);
}
伙计们,请帮我解决这个问题。非常感谢
答案 0 :(得分:1)
var data = [
{
"Id":1,
"userId":"dd1789f1-bb28-4f2c-975a-bad0e3d127cd",
"email":"lalaland@gmail.com",
"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":"lalaland@gmail.com",
"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":"lalaland@gmail.com",
"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"
}
];
// an array of hDays combo so we won't have to set each one manually
var hDays = ["hr_Sun", "hr_Mon", "hr_Tue", "hr_Wed", "hr_Thu", "hr_Fri", "hr_Sat"];
// get the task_data of an item
function getTaskData(item){
var obj = {}; // the task_data placeholder
obj["taskName"] = item["taskName"]; // set the name of this task_data
var tdd = {}; // task_detail_data placeholder
hDays.forEach(function(hday){ // automate the assignments using the above array (or write 8 lines of code assigning them manually)
tdd[hday] = item[hday]; // tdd and item will have the same keys (your question says tdd should have keys like 'h_sun', 'h_mon'... and not like the original keys ('hr_Sun', 'hr_Mon'...) which I assumed it was an orthograph error. If my assumption is not right, some changes here are required)
});
// I don't know why you want this to be an array that contain only one object.
obj["task_detail_data"] = [tdd];
return obj;
}
// Accumulate the result while looping through the items of 'data'
var result = data.reduce(function(acc, item){
// Check we already stored element with projName equal to this item projName
var found = acc.find(function(e){
return e["projName"] == item["projName"];
});
// If we already have, then get this item task_data and push it into the array of task_data of this projName
if(found)
found["task_data"].push(getTaskData(item));
// If not, then push an element having this projName
else{
acc.push({
"projName": item["projName"],
"task_data": [getTaskData(item)] // must be an array so we can push to it further task_data if ever we encounter an item with the same projName
});
}
return acc; // return the accumulator
}, []);
//result is an array of projects. If you want the format in the question use this (although I don't see the reason why):
var theCorrectResult = {
"projects": result
};
console.log(theCorrectResult);
答案 1 :(得分:0)
试试这个代码段:
var arr = [
{
"Id": 1,
"userId": "dd1789f1-bb28-4f2c-975a-bad0e3d127cd",
"email": "lalaland@gmail.com",
"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": "lalaland@gmail.com",
"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": "lalaland@gmail.com",
"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);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>
&#13;