Reputation: 1509
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?
Upvotes: 0
Views: 44
Reputation: 1509
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]);
});
Upvotes: 0
Reputation: 10237
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 }
})
Upvotes: 1