I have this list of objects -
var o_list = [{id:1,name:jo},{id:2,name:mo}];
and I want to extract something like this -
var list = [1,2]
Is there anything already built or how can we implement this.
I have this list of objects -
var o_list = [{id:1,name:jo},{id:2,name:mo}];
and I want to extract something like this -
var list = [1,2]
Is there anything already built or how can we implement this.
You can just map it
var o_list = [{id:1,name:"jo"},{id:2,name:"mo"}];
var list = o_list.map(x => x.id);
document.body.innerHTML = '<pre>' + JSON.stringify(list, 0, 4) + '</pre>';
you can do this using filters https://docs.angularjs.org/api/ng/filter/filter
var myApp = angular.module('myApp', []);
myApp.controller("myCtrl", ['$scope', '$filter',
function($scope, $filter) {
$scope.values = [{
id: 1,
name: 'asdas'
}, {
id: 2,
name: 'blabla'
}];
// this is how you use filters in your script
console.log($filter('extract')($scope.values));
}
]);
myApp.filter('extract', function() {
return function(input) {
return input.map(x => x.id);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<ol ng-repeat="id in values | extract">
<li ng-bind="id"></li>
</ol>
</div>
</div>
You can use only javscript forEach
array method to get desired result
var o_list = [{id:1,name:'jo'},{id:2,name:'mo'}];
var list =[];
o_list.forEach(function(item){
list.push(item.id)
})
console.log(list)