2016-11-10 97 views
0

可以使用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,你看到不適合這份工作的合適工具的循環。有什麼建議麼?

回答

1

它看起來像你有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 
 
     } 
 
    }] 
 
    ] 
 
};

+0

感謝隨便看看。我沒有重複的密鑰,我只是不想輸出所有我收到的真實數據。該物業/鑰匙是獨一無二的。 – user3125823