如何向集合中添加元素

how to add elements to a collection

我是下划线新手,正在尝试实现 below:I 查看集合和结果集合:

 $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}];

有人可以阐明如何使用下划线实现这一点。

使用过滤器:

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

Map across the people collection and extend每个人对应的国家:

    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);
    });