我是新的强调并尝试实现以下目标:我有一个查找集合和结果集合:
$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}];
有人可以说明如何使用下划线实现这一目标。
答案 0 :(得分:1)
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);
});
答案 1 :(得分:0)
使用过滤器:
$scope.result = _.filter($scope.people, function(person) {
person.country === 'USA'; // or some other value
});