2017-05-15 6 views
0

두 객체 배열 hListsList이 있습니다. 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.idsList.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"}]; 

답변

0

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"}]; 
 
    
 
    
 
sList = sList.map((item, index) => { 
 
    item.hId = hList[index] ? hList[index].id : undefined; 
 
    return item 
 
}) 
 

 
console.log(sList);

+0

위대한 일을했다. 나는'map' 함수를 읽어야 할 것이다. 감사! – Rilcon42