Javascript java脚本将嵌套对象提升到根级别
我得到了一个json对象,如下所示:Javascript java脚本将嵌套对象提升到根级别,javascript,lodash,Javascript,Lodash,我得到了一个json对象,如下所示: { "total": 298, "max_score": 5.2472496, "hits": [ { "_index": "kafkajmx2", "_type": "logs", "_id": "AVo-VsUdponm9zQGYYS5", "_score": 5.2472496, "_source": { "metric_value_number": 134
{
"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"
}
}
]
}
{
"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"
}
我要做的是将\u source
对象调到根级别,如下所示:
{
"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"
}
}
]
}
{
"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中是否也有实现这一点的方法
我试过这样做,但我认为\u源代码中的\u
会影响代码吗
var metricData = [];
for (var i=0; i < response.length; i++) {
metricData.push(response[i]._source);
console.log("metric data: ", metricData);
};
var metricData=[];
对于(变量i=0;i
您忘记选择“点击”了吗
请尝试以下操作:
metricData = response.hits.map(hit => hit["_source"])
你忘了选择“点击率”了吗
请尝试以下操作:
metricData = response.hits.map(hit => hit["_source"])
你的问题中没有数组metricData
。对不起,忘了包括这个,我在代码中有一个var metricData=[]
,做了编辑…见下面的答案,看起来是正确的。你的问题中没有数组metricData
。对不起,忘了包括这个,我有一个var metricData=[]
在代码中…进行了编辑…查看下面的答案,看起来是正确的。我在尝试…TypeError:response.hits.map不是一个函数时出现了此错误。
响应对象是什么?您没有提到这一点。我怀疑您应该从中选择json数据。或者您给了我们错误的响应格式和“hits”不是数组。对不起…我的输入错误…再一次…我在这里工作了一天…但它起了作用,非常感谢!我在尝试时遇到了这个错误…TypeError:response.hits.map不是一个函数
响应对象是什么?你没有提到这一点。我怀疑你应该从中选择json数据。或者你给了我们错误的响应格式和“hits”不是数组。对不起…我的错别字…再一次…在这里呆了一天…但它成功了,非常感谢!