简体   繁体   English

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

[英]How can I get specific values using a key from json using AngularJS foreach

I have JSON object returned from elastic search like below 我有从弹性搜索返回的JSON对象,如下所示 在此处输入图片说明

How can i get the Agent and calls value from this JSON. 我如何从该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);
              });;

The above prints the same objects again and again. 上面一次又一次地打印相同的对象。

Somewhere you need to filters the keys if you only want the "Agents" and "Calls" ones. 如果只需要“座席”和“呼叫”,则需要在某些地方过滤键。

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

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

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