简体   繁体   中英

how can i retrieve specific fields from object in javascript?

I want to retrieve a part of an JavaScript objet.

The final object should have all the hits.hits._source I tried different things with lodash or underscore but I did not get it

My inital object is

 {
     "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"
                 }
             }
         ]
     }
 }

I want to get

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

Check this fiddle: http://jsfiddle.net/7jLz13n6/

You can do like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM