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

时间:2015-02-23 08:23:06

标签: javascript elasticsearch lodash

我想检索JavaScript对象的一部分。

最终对象应该包含所有 hits.hits._source  我用lodash或下划线尝试了不同的东西,但我没有得到它

我的初始对象是

 {
     "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": "liz@google.com"
                 }
             },
             {
                 "_index": "users",
                 "_type": "user",
                 "_id": "2",
                 "_score": 1,
                 "_source": {
                     "id": "2",
                     "type": "pro",
                     "email": "john@google.com"
                 }
             }
         ]
     }
 }

我想要

[
    {
        "id": "112",
        "type": "pro",
        "email": "liz@google.com"
    },
    {
        "id": "2",
        "type": "pro",
        "email": "john@google.com"
    }
]

1 个答案:

答案 0 :(得分: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);