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

时间:2017-03-26 07:35:40

标签: javascript angularjs arrays json

我有外部JSON文件调用数据。这是该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"}
]

我想根据以下数组过滤此JSON文件中的数据" b"值。(b0,b1,b3等)

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

示例:

这个数组有b0,b1,b2和b3这些值是1,2,5和7.因此我想从数据JSON文件中只获取1,2,5,7个值数组并显示该数组的文本值。

此阵列可以使用相同的格式进行更改。因此我想考虑b +"数字"参数。

示例1:

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

示例2:

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

示例3:

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

我使用像这样的angularjs获取JSON外部文件,

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

使用repeat显示值。

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

仅显示HTML正文

AAA BBB EEE GGG

2 个答案:

答案 0 :(得分:2)

一种方法是过滤,映射和/或减少具有"bX"值的数组,以创建ID的查找表,然后根据该数组筛选主data数组查找表。除了“数组”不是数组,它是一个普通的对象,所以你不能直接使用数组方法。所以我正在调用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)

答案 1 :(得分:0)

您可以使用javascript原型函数mapfind来过滤数据。 首先将批处理属性获取到数组并映射数组以查找相关值

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