我已使用puppeteer抓取了该对象,但它包含的信息比我实际需要的更多。如何在“组件”中访问“高度”?我已经搜寻了一段时间,但没有找到有效的解决方案。
{
"components": [{
"height": "1.8",
"period": 11,
"trueDirection": 139,
"compassDirection": "SE"
}, {
"height": "5.5",
"period": 8,
"trueDirection": 72,
"compassDirection": "ENE"
}, {
"height": "1",
"period": 13,
"trueDirection": 207,
"compassDirection": "SSW"
}],
"unit": "ft",
"title": "2-3<small class=\"unit\">ft</small>\n",
"fadedRating": 0,
"solidRating": 1,
"time": "Noon",
"date": "Thu 14/02",
"isBigWave": false
}
答案 0 :(得分:2)
要提取包含所有height
值的数组,请像这样使用map
:
const data = {
"components": [{
"height": "1.8",
"period": 11,
"trueDirection": 139,
"compassDirection": "SE"
}, {
"height": "5.5",
"period": 8,
"trueDirection": 72,
"compassDirection": "ENE"
}, {
"height": "1",
"period": 13,
"trueDirection": 207,
"compassDirection": "SSW"
}],
"unit": "ft",
"title": "2-3<small class=\"unit\">ft</small>\n",
"fadedRating": 0,
"solidRating": 1,
"time": "Noon",
"date": "Thu 14/02",
"isBigWave": false
};
const heights = data.components.map(e => e.height);
console.log(heights);
答案 1 :(得分:1)
在这里,我使用.forEach遍历components数组,然后可以轻松访问每个高度。
const obj = {
"components": [{
"height": "1.8",
"period": 11,
"trueDirection": 139,
"compassDirection": "SE"
}, {
"height": "5.5",
"period": 8,
"trueDirection": 72,
"compassDirection": "ENE"
}, {
"height": "1",
"period": 13,
"trueDirection": 207,
"compassDirection": "SSW"
}],
"unit": "ft",
"title": "2-3<small class='\\unit\\'>ft</small>\\n",
"fadedRating": 0,
"solidRating": 1,
"time": "Noon",
"date": "Thu 14/02",
"isBigWave": false
}
obj.components.forEach(c => console.log(c.height))