0
我有兩個對象的數組,hList
和sList
。我試圖給sList
中的每個對象一個匹配hList.ID
的屬性(如果存在並且尚未設置)。我有點糾結於邏輯,但有人可以幫我嗎?如何將對象的屬性與對象數組的屬性進行匹配?
var sList=[{name:"s1",id:"a"},{name: "s2",id:"b"},{name: "s3",id:"c"},{name: "s4",id:"d"}];
var hList=[{name: "h1",id:"x"},{name: "h2",id:"y"},{name: "h3",id:"z"}];
到目前爲止我有:
try{
for(i in sList)
{
var s=sList[i];
//find hList object that matches sList.id value
var h=_.filter(hList.id,function(i){return(i==s.hID)});
//if no hList.id match the sList.hID
//find hList object that doesnt match any
//set the source id to the hList object id
}
}catch(err){console.log("err: ",err);}
目標:(只有一個hList.id
可以匹配sList.hID
如果hList.id
犯規存在sList.hID
應該是不確定的。)
var sList=[{name:"s1",id:"a",hID:"x"},{name: "s2",id:"b",hID="y"},{name: "s3",id:"c",hID="z"},{name: "s4",id:"d",hID=undefined}]; //goal: set sList.hID to match an hList.id
var hList=[{name: "h1",id:"x"},{name: "h2",id:"y"},{name: "h3",id:"z"}];
這很好,我將不得不閱讀'map'函數。謝謝! – Rilcon42