简体   繁体   中英

Dealing with Objects from ElasticSearch - Do I need to reconstruct it?

So I'm using a library called Reactive which works with Elastic Search to display results and set queries. I'm creating my own custom dropdown list with ReactiveComponent and when I use a range with a custom label.. meaning, setting `Keyed: true`, ( Reference, take a look at Keyed Response and the response object ) I no longer get an array rather I get an object which is making it difficult to display the items I want.

So the response I get looks like:

{
  2000 - 2005: { from: 2000, to: 2005, doc_count: 32 }
  2006 - 2010: { from: 2006, to: 2010, doc_count: 77 }
  2011 - 2015: { from: 2011, to: 2015, doc_count: 94 }
  2016 - 2018: { from: 2016, to: 2018, doc_count: 28 }
}

But in order to map over and display the necessary values, I'd like for it to look more like

[{ key: "2016-2018", from: 2016, to: 2018, doc_count: 28} ] (etc)...

Any suggestions on what I should do? Reconstruct the object or go in a different direction?

Basing on the ElasticSearch link you provided won't you get exactly what you want if you just don't specify keyed: true in the request?

Well, if not then use Object.entries :

Object.entries(response).map(([key, value]) => {
  console.log(key) // "2016-2018"
  console.log(value) // { from: 2016, to: 2018, doc_count: 28 }
})

This worked for me:

const reconstructedResults = [];
      const results = aggregations[searchType].buckets;
      Object.keys(results).map(datum => {
        const datapoint = { key: datum, ...results[datum] };
        results[datum] = datapoint;
        return reconstructedResults.push(results[datum]);
      });

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