gpt4 book ai didi

javascript - 如何在 js 中正确制作 .map

转载 作者:行者123 更新时间:2023-11-30 09:11:07 24 4
gpt4 key购买 nike

谁能帮我提取所有带有href 的链接?

这是 map 代码:

const image = images.map(img => img.links)

{ image.map(a => a.map( b => b.href))}

还有其他方法吗?

这是数据

{
'item': [
{
'data': [
{'href': link}
]
},
...
{
'data': [
{'href': link}
]
},
]
}

最佳答案

如果你有以下结构,那么使用flatmap函数:

const data = [
{
'item': [
{
'data': [
{ 'href': 'link' },
{ 'href': 'link2' }
]
}
]
},
{
'item': [
{
'data': [
{ 'href': 'link' },
{ 'href': 'link3' },
]
},
]
}
];

const result = data.flatMap(d =>
d.item.flatMap(i =>
i.data.map(h => h.href)));

console.log(result);

关于javascript - 如何在 js 中正确制作 .map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58888642/

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