如何从javascript中的对象检索特定的字段?

问题描述:

我想要检索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

检查此琴: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);