Find object by its property in array of objects with AngularJS way
Solution 1:
you can use angular's filter
https://docs.angularjs.org/api/ng/filter/filter
in your controller:
$filter('filter')(myArray, {'id':73})
or in your HTML
{{ myArray | filter : {'id':73} }}
Solution 2:
How about plain JavaScript? More about Array.prototype.filter()
.
var myArray = [{'id': '73', 'name': 'john'}, {'id': '45', 'name': 'Jass'}]
var item73 = myArray.filter(function(item) {
return item.id === '73';
})[0];
// even nicer with ES6 arrow functions:
// var item73 = myArray.filter(i => i.id === '73')[0];
console.log(item73); // {"id": "73", "name": "john"}
Solution 3:
For complete M B
answer, if you want to access to an specific attribute of this object already filtered from the array in your HTML, you will have to do it in this way:
{{ (myArray | filter : {'id':73})[0].name }}
So, in this case, it will print john
in the HTML.
Regards!