user818700
user818700

Reputation:

Filter JSON data based on certain properties and values

I have the following JSON object:

{
  'name' : 'John',
  'friends' : [
    {
      'id' : 1,
      'name' : 'George',
      'level' : 10
    },
    {
      'id' : 2,
      'name' : 'Stacy',
      'level' : 8
    },
    {
      'id' : 3,
      'name' : 'Fred',
      'level' : 10
    },
    {
      'id' : 4,
      'name' : 'Amy',
      'level' : 7
    },
    {
      'id' : 5,
      'name' : 'Bob',
      'level' : 10
    }
  ]
}

So as you can see, you have an object (person?) that has a name, and an array of friend objects. Each friend object has an id, name and level.

What I'd like to do, is select all the level 10 friends out of that array, and into another variable/object called var level10Friends.

I'm using AngularJS and all this needs to happen in my controller, but this doesn't necessarily have to be an AngularJS specific problem, you're welcome to use vanilla JavaScript functions as well.

To be honest I don't even know if this is possible and searching the web doesn't seem to bring anything up about something like this...

Upvotes: 1

Views: 27411

Answers (5)

Kailas
Kailas

Reputation: 7578

Using arrow functions you can do it as:

var filteredFriends = list.friends.filter( p => p.level === 10 );

Upvotes: 1

Rayon
Rayon

Reputation: 36599

Use Array.prototype.filter(). The filter() method creates a new array with all elements that pass the test implemented by the provided function.

var users = {
  'name': 'John',
  'friends': [{
    'id': 1,
    'name': 'George',
    'level': 10
  }, {
    'id': 2,
    'name': 'Stacy',
    'level': 8
  }, {
    'id': 3,
    'name': 'Fred',
    'level': 10
  }, {
    'id': 4,
    'name': 'Amy',
    'level': 7
  }, {
    'id': 5,
    'name': 'Bob',
    'level': 10
  }]
};
var wantedData = users.friends.filter(function(i) {
  return i.level === 10;
});
console.log(wantedData);

Upvotes: 22

Schemiii
Schemiii

Reputation: 366

Try this:

var level10friends = list.friends.filter(function(p){return p.level == 10;});

Upvotes: 1

Hitmands
Hitmands

Reputation: 14189

Have a look!

angular
  .module('test', [])
  .value('user', {
  'name' : 'John',
  'friends' : [{
      'id' : 1,
      'name' : 'George',
      'level' : 10
    },
    {
      'id' : 2,
      'name' : 'Stacy',
      'level' : 8
    },
    {
      'id' : 3,
      'name' : 'Fred',
      'level' : 10
    },
    {
      'id' : 4,
      'name' : 'Amy',
      'level' : 7
    },
    {
      'id' : 5,
      'name' : 'Bob',
      'level' : 10
    }]
  })
  .controller('TestCtrl', function TestCtrl($scope, user) {
    var vm = $scope;
    vm.level10friends = (function(friends, REQUIRED_LEVEL) {
      var res = [];
      
      for(var friend, i = 0, len = friends.length; i < len; i++) {
        friend = friends[i];
        
        if(friend.level === REQUIRED_LEVEL) {
          res.push(friend);
        }
        
      }
      
      return res;
    }).call(this, user.friends || [], 10);
  })
;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<article ng-app="test">
  <div ng-controller="TestCtrl">
    <div ng-bind="level10friends | json"></div>
  </div>  
</article>

Upvotes: 0

Keyboard ninja
Keyboard ninja

Reputation: 755

Just found this plugin this morning http://linqjs.codeplex.com/

Its a way to query JSON using linq function. I LOVE IT ^^

EDIT:

little example for your case:

var jsonArray = {your-json-array}
var level10Friends = Enumerable.From(jsonArray)
    .Where("$.user.level == 10")
    .Select("$.user.name")
    .ToArray();

Upvotes: 0

Related Questions