通过包含对象的多维数组迭代遍历
问题描述:
可以使用AngularJS forEach从ES(v1.5)中迭代此JSON响应吗?通过包含对象的多维数组迭代遍历
{response:[property: value,property: value, hits:{property: value, property: value}, hits: [{property:value,property:value, {property:value}}], hits:[{property:value,property:value, {property:value}}]]}
正如你可以看到响应[]有2个命中数组,两个命中数组都充满了对象。尝试使用angular.forEach遍历它们...但没有太多的运气
我是否需要打破每个匹配数组,并通过自己的forEach运行它?
var multi_match_results = es_return.responses[0].hits.hits;
angular.forEach(multi_match_results), function(value, key) {
...
}
var mlt_results = es_return.responses[1].hits.hits;
angular.forEach(mlt_results), function(value, key) {
...
}
OR
有没有办法通过这些与嵌套foreach来?如果是这样,一些示例代码将非常棒!
UPDATE
下面是实际的数据的小样品从ES
{
"responses": [
{
"took": 38,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 2.8937743,
"hits": [
{
"_index": "query-index1",
"_type": "autocomplete",
"_id": "AVhO-9ifp_gdq4wcr69k",
"_score": 2.8937743,
"_source": {
"suggestions": "term term"
}
},
{
"_index": "query-index1",
"_type": "autocomplete",
"_id": "AVhO-9ifp_gdq4wcr69l",
"_score": 2.8937743,
"_source": {
"suggestions": "term1 term1"
}
}
]
}
},
{
"took": 51,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 317,
"max_score": 3.055048,
"hits": [
{...
返回因此,大家可以在返回对象看到有反应阵列,包含2个独立的命中阵列和那些命中数组包含保存2个查询数据的对象。
我的搜索()返回的结果是,像这样
return searchService.search(vm.searchTerms, vm.currentPage).then(function(es_return) {
var results = es_return.responses;
var totalItems = es_return.responses[0].hits.total;
var totalTime = es_return.responses[0].took;
var numPages = Math.ceil(es_return.responses[0].hits.total/vm.itemsPerPage);
vm.results.pagination = [];
for (var i = 0; i < results.length; i++) {
console.log(results);
for (var j = 0; j < results.length; j++) {
console.log(results);
vm.results.totalItems = totalItems;
console.log(vm.results.totalItems);
vm.results.queryTime = totalTime;
vm.results.pagination = searchService.formatResults(es_return.responses[0].hits.hits);//load first 100 results
vm.results.documents = vm.results.pagination.slice(vm.currentPage, vm.itemsPerPage);
console.log(vm.results.documents);
vm.results.mlt = es_return.responses[1].hits.hits;
}
}
的2,你看到不适合这份工作的合适工具的循环。有什么建议么?
答
它看起来像你有JSON是无效的JSON,因为你有重复的键。您可能需要重新考虑生成json值的方式。
这里是关于类似的路线后:
https://stackoverflow.com/a/38267020/6794233
会发生什么,你重复,当你有重复键是 这里。
https://stackoverflow.com/a/5306792/6794233
var responseObj = {
response: [property: value, property: value, hits: {
property: value,
property: value
},
hits: [{
property: value,
property: value,
{
property: value
}
}],
hits: [{
property: value,
property: value,
{
property: value
}
}]
]
};
感谢随便看看。我没有重复的密钥,我只是不想输出所有我收到的真实数据。该物业/钥匙是独一无二的。 – user3125823