【发布时间】:2020-01-26 07:16:20
【问题描述】:
下面是一个 JSON 数组,我想使用 jquery 将其更改为二维数组。在给定的 JSON 格式中,我需要根据它们的 jobNumber 和 activityName 对项目进行分组。而且内部数组必须基于item创建。
[
{
"vehicleHistoryList": [
{
"jobNumber": "X9001027",
"activityName": "5K SERVICE",
"item": "Labour",
"description": "Vincent Esmas",
},
{
"jobNumber": "X9001027",
"activityName": "5K SERVICE",
"item": "Material",
"description": "OIL FILTER COROLLA",
},
{
"jobNumber": "X9001027",
"activityName": "5K SERVICE",
"material": "Material",
"description": "10W40 HELIX HX7 ENGINE OIL - SHELL (AUTO EXPRESS)",
},
{
"jobNumber": "X9001124",
"activityName": "PERIODIC CHECK UP",
"item": "Material",
"description": "TERMINAL ASSY INNOVA",
},
{
"jobNumber": "X9001124",
"activityName": "BATTERY TERMINALS",
"item": "Labour",
"description": "Muslim Shah Abdullah Shah",
},
{
"jobNumber": "X9002578",
"activityName": "5K SERVICE",
"item": "Material",
"description": "RIM (ALLOY )FOR YARIS 2016",
},
{
"jobNumber": "X9002578",
"activityName": "5K SERVICE",
"item": "Material",
"description": "TYRE 185/60 R15-MAXXIS",
},
{
"jobNumber": "X9002578",
"activityName": "5K SERVICE",
"item": "Labour",
"description": "Muhammad Akbar Hussain Shah",
},
{
"jobNumber": "X9002578",
"activityName": "PERIODIC CHECK UP",
"item": "Material",
"description": "RIM (ALLOY )FOR YARIS 2016",
},
{
"jobNumber": "X9002578",
"activityName": "PERIODIC CHECK UP",
"item": "Labour",
"description": "Muhammad Akbar Hussain Shah",
},
{
"jobNumber": "X9002578",
"activityName": "PERIODIC CHECK UP",
"item": "Labour",
"description": "Junaid Ali",
}
]
}
]
如果item 值为Material,则需要将description 值添加到材质数组节点。
如果item 值为Labour,则需要将description 值添加到Labor 数组节点。
以下是所需格式。
[
{
"vehicleHistoryList": [
{
"jobNumber": "X9001027",
"activities": [
{
"activityName": "5K SERVICE",
"material":[
{"OIL FILTER COROLLA"},{"10W40 HELIX HX7 ENGINE OIL - SHELL (AUTO EXPRESS)"}
],
"labours":[
{"Vincent"}
]
}
]
},
{
"jobNumber": "X9001124",
"activities": [
{
"activityName": "PERIODIC CHECK UP",
"material":[
{"TERMINAL ASSY INNOVA"}
],
"labours":[
{"Muslim Shah"}
]
}
]
},
{
"jobNumber": "X9002578",
"activities": [
{
"activityName": "5K SERVICE",
"material":[
{"RIM (ALLOY )FOR YARIS 2016"},{"TYRE 185/60 R15-MAXXIS"}
],
"labours":[
{"Muhammad Akbar"},{"Hussain Shah"}
]
},
{
"activityName": "PERIODIC CHECK UP",
"material":[
{"RIM (ALLOY )"}
],
"labours":[
{"Muhammad Hussain"},{"Junaid Ali"}
]
}
]
}
]
}
]
注意:不同的作业编号可以有相同的活动名称。
下面是我用过的代码:
var jobNum = []
var jobList = []
var activityName = []
var activityList = []
for(var i=0; i<vehicleHistoryList.length;i++) {
if(jobNum.indexOf(vehicleHistoryList[i].jobNumber)==-1) {
jobNum.push(vehicleHistoryList[i].jobNumber)
jobList.push({jobNum:vehicleHistoryList[i].jobNumber,activities:[]})
if(activityName.indexOf(vehicleHistoryList[i].activityName)==-1) {
activityName.push(vehicleHistoryList[i].activityName)
activityList.push({jobNum:vehicleHistoryList[i].jobNumber,activityName:vehicleHistoryList[i].activityName,material:[],labour:[]})
var index = activityList.map(function (img) { return img.activityName; }).indexOf(vehicleHistoryList[i].activityName);
if(vehicleHistoryList[i].material =="Material") {
activityList[index].material.push(vehicleHistoryList[i].description);
} else {
activityList[index].labour.push(vehicleHistoryList[i].description);
}
} else {
var index = activityList.map(function (img) { return img.activityName; }).indexOf(vehicleHistoryList[i].activityName);
if(vehicleHistoryList[i].material =="Material") {
activityList[index].material.push(vehicleHistoryList[i].description);
} else {
activityList[index].labour.push(vehicleHistoryList[i].description);
}
}
} else {
var index = jobList.map(function (img) { return img.jobNum; }).indexOf(vehicleHistoryList[i].jobNumber);
console.log(index)
if(activityName.indexOf(vehicleHistoryList[i].activityName)==-1) {
activityName.push(vehicleHistoryList[i].activityName)
activityList.push({jobNum:vehicleHistoryList[i].jobNumber,activityName:vehicleHistoryList[i].activityName,material:[],labour:[]})
var index = activityList.map(function (img) { return img.activityName; }).indexOf(vehicleHistoryList[i].activityName);
if(vehicleHistoryList[i].material =="Material") {
activityList[index].material.push(vehicleHistoryList[i].description);
} else {
activityList[index].labour.push(vehicleHistoryList[i].description);
}
} else {
var index = activityList.map(function (img) { return img.activityName; }).indexOf(vehicleHistoryList[i].activityName);
if(vehicleHistoryList[i].material =="Material") {
activityList[index].material.push(vehicleHistoryList[i].description);
} else {
activityList[index].labour.push(vehicleHistoryList[i].description);
}
}
}
}
我在这里面临的问题是,如果相同的 activityName 进入不同的 jobNumber,则所有相应的劳动力和材料都添加到同一个节点。
请帮我解决这个问题,我们将不胜感激。谢谢
【问题讨论】:
-
我认为否决票是因为您没有给出明确的问题陈述。您没有指出您当前拥有的代码哪里出错了。
-
@RobbyCornelissen 用我的代码问题编辑了问题。
-
@RobbyCornelissen 谢谢 :)
标签: jquery arrays json parsing multidimensional-array