简体   繁体   English

如何根据指定的参数AngularJS过滤JSON数据

[英]How to filter JSON data according to specified parameter AngularJS

I have external JSON file call datas. 我有外部JSON文件调用数据。 This is the body of that JSON file. 这是该JSON文件的正文。

[
    {"value": "1", "text": "aaa"},
    {"value": "2", "text": "bbb"},
    {"value": "3", "text": "ccc"},
    {"value": "4", "text": "ddd"},
    {"value": "5", "text": "eee"},
    {"value": "6", "text": "fff"},
    {"value": "7", "text": "ggg"},
    {"value": "8", "text": "hhh"},
    {"value": "9", "text": "iii"},
    {"value": "10", "text": "jjj"}
]

I want to filter data from this JSON file according to following array "b" values.(b0, b1, b3 etc) 我想根据以下数组“ b”值从此JSON文件过滤数据。(b0,b1,b3等)

$scope.array = {"badge":"1,2,5,7","id":"0","b0":"1","b1":"2","b2":"5","b3":"7"}

Example: 例:

This array have b0, b1, b2 and b3 those values are 1, 2, 5 and 7. Therefor I want to get only 1, 2, 5, 7 values arrays from datas JSON file and display text values of this array. 该数组具有b0,b1,b2和b3,这些值分别是1、2、5和7。因此,我想从数据JSON文件中仅获取1、2、5、7值数组,并显示该数组的文本值。

This array can be change with same format. 可以使用相同的格式更改此数组。 Therefor I want to consider b+"number" parameters. 因此,我想考虑b +“ number”参数。

Example 1: 范例1:

$scope.array = {"badge":"1,2,3,9","id":"0","b0":"1","b1":"2","b2":"3","b3":"9"}

Example 2: 范例2:

$scope.array = {"badge":"1,2,7","id":"0","b0":"1","b1":"2","b2":"7"}

Example 3: 范例3:

$scope.array = {"badge":"1,2,5,7,8,9","id":"0","b0":"1","b1":"2","b2":"5","b3":"7","b4":"8","b5":"9"}

I get that JSON external file using angularjs like this, 我像这样用angularjs来获取JSON外部文件,

$http.get("/json/datas.json").success(function(data) {
      $scope.datas= data;
});

Values are display using repeat. 值使用重复显示。

<div ng-repeat="data in datas">
    <span ng-bind-html="data.text"></span>
</div>

Display HTML body only 仅显示HTML正文

aaa bbb eee ggg aaa bbb eee ggg

One way to do it is to filter, map, and/or reduce the array that has the "bX" values to create a lookup table of IDs, then filter the main data array based on that lookup table. 一种方法是过滤,映射和/或缩小具有"bX"值的数组以创建ID的查找表,然后根据该查找表过滤主data数组。 Except that that "array" isn't an array, it is a plain object, so you can't use array methods on it directly. 除了“数组”不是数组之外,它是一个普通对象,因此您不能直接在其上使用数组方法。 So I'm calling Object.keys() to get its keys in an array, and then I've chosen to use .reduce() to create the lookup table based on the keys that have the right format: 因此,我要调用Object.keys()以将其键放入数组中,然后选择使用.reduce()基于具有正确格式的键创建查找表:

 var data = [ {"value": "1", "text": "aaa"}, {"value": "2", "text": "bbb"}, {"value": "3", "text": "ccc"}, {"value": "4", "text": "ddd"}, {"value": "5", "text": "eee"}, {"value": "6", "text": "fff"}, {"value": "7", "text": "ggg"}, {"value": "8", "text": "hhh"}, {"value": "9", "text": "iii"}, {"value": "10", "text": "jjj"} ] var $scope = {} // demo $scope object $scope.array = {"badge":"1,2,5,7","id":"0","b0":"1","b1":"2","b2":"5","b3":"7"} var bVals = Object.keys($scope.array).reduce(function(a, c) { if (/^b\\d+$/.test(c)) a[$scope.array[c]] = true return a }, {}) console.log(bVals) var filteredData = data.filter(function(v) { return bVals[v.value] }) console.log(filteredData) 

You can use javascript prototype functions map and find to filter the data. 您可以使用javascript原型函数mapfind来过滤数据。 First get the batch properties to an array and map the array to find the relevant values 首先将批处理属性获取到数组并映射该数组以找到相关值

$scope.array = {"badge":"1,2,3,9","id":"0","b0":"1","b1":"2","b2":"3","b3":"9"}
var batchArr = $scope.array.badge.split(',');
$scope.result = batchArr.map(o=> $scope.datas.find(k=> k.value == o)) 

 angular.module("app",[]) .controller("ctrl",function($scope,$sce){ $scope.datas = [ {"value": "1", "text": "aaa"}, {"value": "2", "text": "bbb"}, {"value": "3", "text": "ccc"}, {"value": "4", "text": "ddd"}, {"value": "5", "text": "eee"}, {"value": "6", "text": "fff"}, {"value": "7", "text": "ggg"}, {"value": "8", "text": "hhh"}, {"value": "9", "text": "iii"}, {"value": "10", "text": "jjj"} ] $scope.array = {"badge":"1,2,3,9","id":"0","b0":"1","b1":"2","b2":"3","b3":"9"} var batchArr = $scope.array.badge.split(','); $scope.result = batchArr.map(o=> $scope.datas.find(k=> k.value == o)) console.log($scope.result) $scope.trust = function(html){ return $sce.trustAsHtml(html); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <div ng-repeat="data in result"> <span ng-bind-html="trust(data.text)"></span> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM