javascript - How to get value of key 1 where key 2 equals value 2? AngularJS -


i have array object:

color =[{"id":"1", "name":"black"},{"id":"2", "name":"white"}] 

and have value 1 $scope.object.id = 1

so i'm trying color name color id = $scope.object.id. how do this?

i have code in html:

<select ng-model="color" ng-options="color.name color in colorlist" class='material-options'></select>  

how use value default select option?

you use filter method.

var clr = color.filter(function(item){ return item.id === $scope.object.id; }); 

then can read name value below:

var name = clr.length === 1 ? clr[0].name : ''; 

according mdn

the filter() method creates new array elements pass test implemented provided function.

that being sad, why check length of clr. given context, there should 1 color id or none. so, array filter create , return either of length 0 or 1.


Comments