Joey Zhang Joey Zhang - 2 years ago 69
AngularJS Question

How to find an array object according to given information?

I have an array in AngularJS controller like this:

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

I have got a name 'Lucy', how can I get the age of the name in the controller (not in HTML)?

Answer Source

I've created a plunk here that outlines a single result, with just the age, as well as multiple results.

This could also be implemented within a filter, which is documented on the Angular site here:

angular.module('plnk',[]).controller('plnkCtrl', function($scope){

  // Note, I added a second Joey here to test the multiple function.
  // For output, check the 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 = [];
       if ( === personLookup) {
         // or results.push(person.age) for age only
    return results;


  function getAgeSingle(personLookup) {
    var result = '';
       if ( === personLookup && !result) {
         result = person.age;
    return result;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download