gpt4 book ai didi

javascript - 当我创建一个空项目数组时,我不能使用 map

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

我试过这个


> Array(3)
[ <3 empty items> ]
> // and this joins up all the nothings
> Array(3).join('-')
'--'
> // but...
> Array(3).map((x) => 'a')
[ <3 empty items> ]
> // map doesn't work with the array of empty items!
我以为我会得到与此相同的结果
> [undefined, undefined, undefined].map((x) => 'a')
[ 'a', 'a', 'a' ]
那是怎么回事?

最佳答案

您可以使用 :

Array(3).fill(null).map(x => 'a')

关于javascript - 当我创建一个空项目数组时,我不能使用 map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65955791/

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