2015-02-23 118 views
0

我想要檢索JavaScript對象的一部分。如何從javascript中的對象檢索特定的字段?

最終的對象應該具有的所有hits.hits._source 我嘗試不同的事情lodash或下劃線,但我沒有得到它

我inital對象是

{ 
    "took": 7, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 3, 
     "max_score": 1, 
     "hits": [ 
      { 
       "_index": "users", 
       "_type": "user", 
       "_id": "1", 
       "_score": 1, 
       "_source": { 
        "id": "112", 
        "type": "pro", 
        "email": "[email protected]" 
       } 
      }, 
      { 
       "_index": "users", 
       "_type": "user", 
       "_id": "2", 
       "_score": 1, 
       "_source": { 
        "id": "2", 
        "type": "pro", 
        "email": "[email protected]" 
       } 
      } 
     ] 
    } 
} 

我想獲得

[ 
    { 
     "id": "112", 
     "type": "pro", 
     "email": "[email protected]" 
    }, 
    { 
     "id": "2", 
     "type": "pro", 
     "email": "[email protected]" 
    } 
] 
+0

啃削從root.hits.hits 「_source」 – dandavis 2015-02-23 08:25:51

+0

'B = [];對於(i的a.hits.hits){b.push(a.hits .hits [i] ._ source)}''會得到'b'作爲你的答案 – itzMEonTV 2015-02-23 08:29:55

回答

0

檢查此琴:http://jsfiddle.net/7jLz13n6/

可以這樣做:

var output=[]; 
var input=o.hits.hits; 
for(i=0;i<input.length;i++) 
    output.push(input[i]._source); 
console.log(output); 
相關問題