javascript 根据某些属性和值过滤 JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33364851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:25:10  来源:igfitidea点击:

Filter JSON data based on certain properties and values

javascriptjsonangularjs

提问by Hitmands

I have the following JSON object:

我有以下 JSON 对象:

{
  '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 friendobjects. Each friend object has an id, nameand level.

因此,如您所见,您有一个对象(人?),它有一个name, 和一个friend对象数组。每个朋友对象都有一个id,namelevel

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.

我想要做的是从该数组中选择所有级别为 10 的朋友,并将其放入另一个名为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.

我正在使用 AngularJS,所有这些都需要在我的控制器中发生,但这不一定是 AngularJS 特定的问题,欢迎您也使用普通的 JavaScript 函数。

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...

老实说,我什至不知道这是否可能,并且在网上搜索似乎没有提出任何关于这样的事情......

回答by Rayon

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

使用Array.prototype.filter(). filter()方法array使用通过提供的函数实现的测试的所有元素创建一个新元素。

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);

回答by Schemiii

Try this:

试试这个:

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

回答by Kailas

Using arrow functions you can do it as:

使用箭头函数,你可以这样做:

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

回答by Hitmands

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>

回答by Keyboard ninja

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

今天早上刚发现这个插件 http://linqjs.codeplex.com/

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

它是一种使用 linq 函数查询 JSON 的方法。我喜欢它^^

EDIT:

编辑:

little example for your case:

你的情况的小例子:

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