我的挑战是基于uId合并2个数组。
var job = [{
"uId": 1
}, {
"uId": 2
}]
var jobDetails = [{
"uId": 1,
"salary": 5000
}, {
"uId": 2,
"salary": 5000
}]
到目前为止,我坚持
foreach(var job as var k=>var &arr)
{
if(arr->{'uId'}==2)
{
arr->{'salary'}=salary;
}
}
哪个硬编码2可以找到工作数组&#39>。
如何制作类似
的内容var job = [{
"uId": 1,
"salary": [{
"uId": 1,
"salary": 5000
}]
}, {
"uId": 2,
"salary": [{
"uId": 2,
"salary": 5000
}]
}];
答案 0 :(得分:0)
正如聊天中所讨论的,这是一个javascript示例,它构建了您所要求的内容:
var tabs = [{"uId":"2","tabId":1,"tabName":"Main","points":"10","active":"true"},{"uId":"3","tabId":2,"tabName":"Photography","points":"20","active":""}];
var tasks = [{"taskId":3,"taskName":"Sing Sing Gem","priorty":3,"date":"2014-04-25","done":0,"tabId":1,"uId":"2"},{"taskId":4,"taskName":"Shooting","priorty":4,"date":"2014-04-25","done":0,"tabId":2,"uId":"3"}];
var uidSet = {};
var UIDSortFunction = function(a,b){
uidSet[a.uId] = 1;
uidSet[b.uId] = 1;
return a.uId - b.uId;
};
tabs.sort(UIDSortFunction);
tasks.sort(UIDSortFunction);
var endResult = [];
var i, j, tabsLen = tabs.length, tasksLen = tasks.length, k = 0;
for(var key in uidSet)
{
if(uidSet.hasOwnProperty(key))
{
endResult.push({
uId : key,
tabs:[],
tasks:[]
});
for(i = 0; i < tabsLen; ++i)
{
if(tabs[i].uId === key)
endResult[k].tabs.push({
tabId:tabs[i].tabId,
tabName: tabs[i].tabName,
points: tabs[i].points
});
}
for(j = 0; j < tasksLen; ++j)
{
if(tasks[j].uId === key)
endResult[k].tasks.push({
uId: tasks[j].uId,
tabId:tasks[j].tabId,
taskName: tasks[j].taskName
});
}
++k;
}
}
console.log(endResult);