Search code examples
jsonangularjsqueryingangularjs-rootscope

Querying a JSON-Structure in Angular: Find the right index and get a certain property of it


Assume, following JSON Structure is existing:

[
  {
    "role_id": 1,
    "role_name": "Admin"
  },
  {
    "role_id": 2,
    "role_name": "Editor"
  }
]

and stored in $rootScope.roles.

What I need is:

$rootScope.roles[index -> where role_id == 2].rolename  // gets -> Editor

How can I do that in Angular?


Solution

  • ng-lodash is an elegant way:

    role_name = lodash.pluck(lodash.where($rootScope.roles,{'role_id': 2 }),'role_name');