gpt4 book ai didi

javascript - javascript 将嵌套对象带到根级别

转载 作者:行者123 更新时间:2023-12-01 04:00:27 25 4
gpt4 key购买 nike

我得到了一个如下所示的 json 对象:

{
"total": 298,
"max_score": 5.2472496,
"hits": [
{
"_index": "kafkajmx2",
"_type": "logs",
"_id": "AVo-VsUdponm9zQGYYS5",
"_score": 5.2472496,
"_source": {
"metric_value_number": 134,
"path": "/home/usrxxx/logstash-5.2.0/bin/jmxconf",
"@timestamp": "2017-02-14T20:35:58.266Z",
"metric_name": "IsrExpandsPerSec.Count",
"@version": "1",
"host": "localhost",
"metric_type": "ReplicaManager",
"metric_path": "node1.kafka.server:type=ReplicaManager,name=IsrExpandsPerSec.Count",
"type": null,
"metric_node": "node1.kafka.server"
}
},
{
"_index": "kafkajmx2",
"_type": "logs",
"_id": "AVo-V69Yponm9zQGYYwS",
"_score": 5.2472496,
"_source": {
"metric_value_number": 134,
"path": "/home/usrxxx/logstash-5.2.0/bin/jmxconf",
"@timestamp": "2017-02-14T20:36:58.216Z",
"metric_name": "IsrExpandsPerSec.Count",
"@version": "1",
"host": "localhost",
"metric_type": "ReplicaManager",
"metric_path": "node1.kafka.server:type=ReplicaManager,name=IsrExpandsPerSec.Count",
"type": null,
"metric_node": "node1.kafka.server"
}
}
]
}

我想要做的是将 _source 对象提升到根级别,如下所示:

{
"metric_value_number": 134,
"path": "/home/usrxxx/logstash-5.2.0/bin/jmxconf",
"@timestamp": "2017-02-14T20:36:58.216Z",
"metric_name": "IsrExpandsPerSec.Count",
"@version": "1",
"host": "localhost",
"metric_type": "ReplicaManager",
"metric_path": "node1.kafka.server:type=ReplicaManager,name=IsrExpandsPerSec.Count",
"type": null,
"metric_node": "aesdp0101.kafka.server"
},
{
"metric_value_number": 134,
"path": "/home/usrxxx/logstash-5.2.0/bin/jmxconf",
"@timestamp": "2017-02-14T20:35:58.266Z",
"metric_name": "IsrExpandsPerSec.Count",
"@version": "1",
"host": "localhost",
"metric_type": "ReplicaManager",
"metric_path": "node1.kafka.server:type=ReplicaManager,name=IsrExpandsPerSec.Count",
"type": null,
"metric_node": "aesdp0101.kafka.server"
}

我不确定如何在 javascript 中本地执行此操作,或者 lodash 中是否也有办法执行此操作...

我尝试过这样做,但我认为 _source 中的 _ 正在影响代码?

      var metricData = [];
for (var i=0; i < response.length; i++) {

metricData.push(response[i]._source);
console.log("metric data: ", metricData);

};

最佳答案

您是否忘记选择“点击”?

尝试以下操作:

metricData = response.hits.map(hit => hit["_source"])

关于javascript - javascript 将嵌套对象带到根级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42236230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com