如何使用AngularJS foreach使用json中的键获取特定值

时间:2015-01-21 18:20:46

标签: javascript html json angularjs

我有弹性搜索返回的JSON对象,如下所示 enter image description here

如何从此JSON获取代理调用值。

$scope.results = client
                .query(oQuery.query($scope.queryTerm || '*'))
                .doSearch().then(function (body) {
             $scope.results = body.hits.hits;
             var resultstofilter = $scope.results;
             var log = [];
              angular.forEach(results, function(result, key) {
                angular.forEach(result, function(value, key) {
                   this.push(key + ': ' + value);
                 }, log);
              }, log);
             console.log(resultstofilter);
            }, function (error) {
              console.trace(error.message);
              });;

以上一次又一次打印相同的物体。

1 个答案:

答案 0 :(得分:1)

如果您只想要“代理”和“呼叫”,您需要过滤密钥。

$scope.results = client
            .query(oQuery.query($scope.queryTerm || '*'))
            .doSearch().then(function (body) {
         $scope.results = body.hits.hits;
         var resultstofilter = [];

         for (var i=0; i<$scope.results.length; ++i) {
           var result = $scope.results[i];
           resultstofilter[i] = {};
           for (var key in result) {
             // If all you want are keys that doesn't start with '_'
             // you can also test key.substring(0, 1) !== '_'
             if (key === 'Agent' || key === 'Calls') {
               resultstofilter[i][key] = result[key];
             }
           }
         }

         console.log(resultstofilter);
        }, function (error) {
          console.trace(error.message);
        });;