或多或少的情况是这样的:我得到一个数组(使用JS)和一个对象(让他们称之为TASK)看起来像这样(它不是完整的数组,只有一个实例):
{
"id": "28",
"name": "sdfsdf",
"progress": 80,
"description": "",
"code": "1",
"level": 1,
"status": "STATUS_SUSPENDED",
"depends": "",
"canWrite": true,
"start": 1444341600000,
"duration": 7,
"end": 1445291999999,
"startIsMilestone": 0,
"endIsMilestone": 0,
"collapsed": false,
"assigs": [
{
"resourceId": 3,
"otherStuff": xyz
},
{
"resourceId": 2,
"otherStuff": xyz
}
],
"hasChild": true
}
我加载的另一个对象包含所有"资源",在第一个数组中引用" assigs":[](让他们调用这些资源):
[
{
"ID": "1",
"name": "service | 1st resource we need",
"unit": "pcs",
"quantity": "10"
},
{
"ID": "2",
"name": "money | Office space",
"unit": "hour",
"quantity": "50"
},
{
"ID": "3",
"name": "product | Money for nothing...",
"unit": "$",
"quantity": "300"
},
{
"ID": "4",
"name": "people | Chovjek",
"unit": "people",
"quantity": "1"
}
]
我使用任务数据填充了一些表单字段,但我根本无法弄清楚如何连接"这项任务及其资源。
答案 0 :(得分:1)
如果我们正在讨论较新版本的javascript,请使用
for(var task of tasks)
{
for(var ass of task.assigns)
{
for(var res of resources)
{
if(res.ID === ass.resourceId.toString()) {
//here res and ass match and you can do what you want
}
}
}
}
在JS的所有版本中,你都可以这样做
for(var i = 0; i < tasks.length; i++)
{
for(var x = 0; x< tasks[i].assigns.length; x++)
{
for(var y = 0; y < resources.length; y++)
{
if(resources[y].ID === tasks[i].assigns[x].resourceId.toString()) {
//here res and ass match and you can do what you want
}
}
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
如果不是&#34; resourceId&#34;你只是在那里设置你的资源对象? 你将有你的联系:
{
"id": "28",
"name": "sdfsdf",
/* .... */
"assigs": [
{
"resource":
{
"ID": "1",
"name": "service | 1st resource we need",
"unit": "pcs",
"quantity": "10"
},
"otherStuff": xyz
},
{
"resource":
{
"ID": "2",
"name": "service | 2nd resource we need",
"unit": "pcs",
"quantity": "20"
},
"otherStuff": xyz
}
],
"hasChild": true
}
或者,如果你想保留你的结构,只需添加一个对Resources数组中存在的对象的引用:
"assigs":
[
{
"resourceId": 3,
"resource": yourResourceArray[0], //<-- [0] for sake of simplicity
"otherStuff": xyz
},
{
"resourceId": 2,
"resource": yourResourceArray[1], //<-- [1] for sake of simplicity
"otherStuff": xyz
}
],
答案 3 :(得分:-1)
我不确定这是否是您正在寻找的内容,但您应该能够遍历Task
数组,并在该循环内循环遍历{{ 1}}数组,并在此循环内循环遍历assigs
数组。这样做,您可以找到连接。
示例:强>
Resources
注意: 这可能会得到相当大的优化,但至少在这里是
编辑: 或正如Christian Nielsen所做的那样,使用foreach循环