2015-01-10 1 views
0

임 새로운 요소를 추가하는 방법 : 강조하고 아래에 달성하기 위해 노력하는 모음

$scope.Countries:[{Country:UK,City:London,Areacode:567}, 
{Country:USA,City:Texas,Areacode:987}, 
{Country:CHINA,City:XXX,Areacode:XXX}] 

$scope.People= [{Fname:'John',Country:'USA'},{Fname:'Bob',Country:'UK'},]; 

내가가 국가 필드를 추가하는 하나의 콜렉션을 원하는 : 내가 찾아 수집 및 결과 컬렉션이 다음과 같이 매핑 된 국가에 따라 사람들 모음 :

$scope.Result=[{Fname:'John',Country:'USA',City:Texas,Areacode:987} 
,{Fname:'Bob',Country:'UK',City:London,Areacode:567}]; 

사람이 사용 밑줄을 달성하는 방법에 대한 몇 가지 빛을 던질 수 있습니다.

답변

1

사람들 모음에서 Mapextend 해당 국가와 각 사람 : 응답에 대한

var countries = [ 
     {Country:'UK', City:'London', Areacode:567}, 
     {Country:'USA', City:'Texas', Areacode:987} 
    ]; 

    var people = [ 
     {Fname:'John', Country:'USA'}, 
     {Fname:'Bob', Country:'UK'}, 
     {Fname:'Jane', Country:'UK'}, 
     {Fname:'Dai', Country:'WALES'} 
    ]; 

    var result = _.map(people, function(person){ 
     var country = _.findWhere(countries, { Country: person.Country }); 
     return _.extend(person, country); 
    }); 
+0

고마워,'code' 아래에서 같은 것을 얻었습니다 var some_map = _ .지도($ scope.People, function (item) { var child = _. 여기서 ($ scope.Countries, {Country : item.Country}); item.City = child [0] .City; item.AreaCode = child [0] .Areacode; 반품 항목 : }}); 하지만 UR 코드는 훨씬 간단하고 깔끔합니다 ... – Andrea

0

사용 필터 :

$scope.result = _.filter($scope.people, function(person) { 
    person.country === 'USA'; // or some other value 
}); 
+0

감사, 필터는 수집하지만 임에서 일치하는 행을 발생합니다 for 루프를 사용하지 않고 새로운 컬렉션을 만드는 대신에 두 가지 컬렉션을 간단한 방법으로 결합하는 방법을 알고 싶습니다 ... – Andrea