我有弹性搜索返回的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);
});;
以上一次又一次打印相同的物体。
答案 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);
});;