You should use Array.prototype.reduce to do this. I did do a little JS perf test to verify that this is more performant than doing a .filter
+ .map
.
$scope.appIds = $scope.applicationsHere.reduce(function(ids, obj){
if(obj.selected === true){
ids.push(obj.id);
}
return ids;
}, []);
Just for the sake of clarity, here's the sample .reduce
I used in the JSPerf test:
var things = [_x000D_
{id: 1, selected: true},_x000D_
{id: 2, selected: true},_x000D_
{id: 3, selected: true},_x000D_
{id: 4, selected: true},_x000D_
{id: 5, selected: false},_x000D_
{id: 6, selected: true},_x000D_
{id: 7, selected: false},_x000D_
{id: 8, selected: true},_x000D_
{id: 9, selected: false},_x000D_
{id: 10, selected: true},_x000D_
];_x000D_
_x000D_
_x000D_
var ids = things.reduce((ids, thing) => {_x000D_
if (thing.selected) {_x000D_
ids.push(thing.id);_x000D_
}_x000D_
return ids;_x000D_
}, []);_x000D_
_x000D_
console.log(ids)
_x000D_
EDIT 1
Note, As of 2/2018 Reduce + Push is fastest in Chrome and Edge, but slower than Filter + Map in Firefox