angularjs - How to find an array object according to given information? -


i have array in angularjs controller this:

$scope.persons = [{name:'joey', age:'27'},{name:'lucy', age:'22'}] 

i have got name 'lucy', how can age of name in controller (not in html)?

i've created plunk here outlines single result, age, multiple results.

this implemented within filter, documented on angular site here: https://docs.angularjs.org/api/ng/filter/filter

https://plnkr.co/edit/ofrmzpqrzftonafyjp7z?p=info

angular.module('plnk',[]).controller('plnkctrl', function($scope){    // note, added second joey here test multiple function.   // output, check browser console.    $scope.persons = [{name:'joey', age:'27'},{name:'joey', age:'28'},{name:'lucy', age:'22'}]    console.log('single -> ', getagesingle('lucy'));   console.log('multiple ->',getagemultiple('joey'));    function getagemultiple(personlookup) {      var results = [];     angular.foreach($scope.persons,function(person){        if (person.name === personlookup) {          results.push(person);          // or results.push(person.age) age        }      });     return results;    }    function getagesingle(personlookup) {     var result = '';     angular.foreach($scope.persons,function(person){        if (person.name === personlookup && !result) {          result = person.age;        }      });     return result;   }  }); 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -