[英]Get unkown children of node from a JSONPath in Javascript
I am currently building a tool that should extract the Core Web Vitals metrics of a URL.我目前正在构建一个工具,该工具应该提取 URL 的核心 Web Vitals 指标。
With the API i can receive a JSON object wich i can access with JSONPath.使用 API 我可以收到一个 JSON object 我可以使用 JSONPath 访问。
I would like to use a forEach loop to input the data into the HTML-fields.我想使用 forEach 循环将数据输入到 HTML 字段中。
Now my question is, how can i access child nodes of a JSON without using their names.现在我的问题是,如何在不使用名称的情况下访问 JSON 的子节点。
document.querySelectorAll('[data-section^="cww"]').forEach((nodes, index) => {
console.log(values.body.record.metrics);
});
{
"key": {
"origin": "https://developer.mozilla.org"
},
"metrics": {
"cumulative_layout_shift": {
"histogram": [
{
"start": "0.00",
"end": "0.10",
"density": 0.9377813344003197
},
{
"start": "0.10",
"end": "0.25",
"density": 0.039611883565069506
},
{
"start": "0.25",
"density": 0.022606782034610366
}
],
"percentiles": {
"p75": "0.01"
}
},
"first_contentful_paint": {
"histogram": [
{
"start": 0,
"end": 1800,
"density": 0.9419767907162874
},
{
"start": 1800,
"end": 3000,
"density": 0.03741496598639458
},
{
"start": 3000,
"density": 0.02060824329731889
}
],
"percentiles": {
"p75": 841
}
},
"first_input_delay": {
"histogram": [
{
"start": 0,
"end": 100,
"density": 0.9863863863863849
},
{
"start": 100,
"end": 300,
"density": 0.008308308308308296
},
{
"start": 300,
"density": 0.0053053053053052955
}
],
"percentiles": {
"p75": 5
}
},
"largest_contentful_paint": {
"histogram": [
{
"start": 0,
"end": 2500,
"density": 0.9460068054443531
},
{
"start": 2500,
"end": 4000,
"density": 0.03467774219375491
},
{
"start": 4000,
"density": 0.019315452361889692
}
],
"percentiles": {
"p75": 1135
}
}
}
}
try this尝试这个
var p75s = [];
Object.keys(data.metrics).forEach(function (key) {
p75s.push(data.metrics[key].percentiles.p75);
});
output output
["0.01",841,5,1135]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.