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

问题描述 投票:0回答:2

所以我正在使用一个名为Reactive的库,它与Elastic Search一起使用以显示结果和设置查询。我正在用ReactiveComponent创建我自己的自定义下拉列表,当我使用带有自定义标签的范围时...意思是,设置`Keyed:true`,(Reference, take a look at Keyed Response and the response object)我不再得到一个数组而是得到一个正在制作它的对象很难显示我想要的项目。

所以我得到的反应如下:

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

但是为了映射并显示必要的值,我希望它看起来更像

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

关于我应该做什么的任何建议?重建对象还是朝不同的方向前进?

javascript reactjs object elasticsearch
2个回答
1
投票

基于你提供的ElasticSearch链接,如果你没有在请求中指定keyed: true,你会得到你想要的吗?

好吧,如果没有,那么使用Object.entries

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

0
投票

这对我有用:

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]);
      });
© www.soinside.com 2019 - 2024. All rights reserved.