简体   繁体   English

处理来自ElasticSearch的对象-我需要重建它吗?

[英]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. 因此,我正在使用一个名为Reactive的库,该库与Elastic Search配合使用以显示结果和设置查询。 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. 我正在使用ReactiveComponent创建我自己的自定义下拉列表,当我使用带有自定义标签的范围时。.意思是,设置了“ Keyed:true”,(( 参考,看看Keyed Response和Response对象 )我不再一个数组,而不是我得到一个对象,这很难显示我想要的项目。

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? 如果您只是不指定keyed: true ,那么基于您提供的ElasticSearch链接将不会得到所需的结果keyed: true

Well, if not then use Object.entries : 好吧,如果没有,那么使用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]);
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 js / reactjs-如何将json中的数据重构为数组? - js/reactjs - How do i reconstruct data from a json into a array? 我有一个扁平数组作为输入,我需要以树格式重建它 - I have a flattened array as input, I need to reconstruct it in a tree format 我需要在对象中转换这个数组吗? - Do I need to convert this Array in Objects? 使用 Javascript 我如何将这个对象数组解构和重建为 json 对象 - Using Javascript how would I deconstruct and reconstruct this array of objects to json objects 处理对象时需要帮助理解函数绑定 - Need help understanding function bind when dealing with objects 挖掘子对象并重建数据 - digging into child objects and reconstruct the data 如何将请求中的原始标头重建为数组? - How can I reconstruct raw headers from request into an array? 为什么我需要从另一个来源指向Array构造函数,为什么我不能直接在数组对象上调用toString()? - Why do I ever need to point the Array constructor from another source and why can't I call toString() directly on array objects? 我是否需要在窗口卸载时显式销毁JavaScript对象? - Do I need to explicitly destroy JavaScript objects on window unload? 我需要担心吗? -“公开Chrome JS对象...” - Do I need to Worry? - “Exposing chrome JS objects…”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM