gpt4 book ai didi

javascript - 默认数组值

转载 作者:IT王子 更新时间:2023-10-29 03:19:32 26 4
gpt4 key购买 nike

有没有办法在 javascript 中为数组分配默认值?

例如:一个有 24 个槽的数组,默认为 0

最佳答案

您可以在数组上使用 fill 函数:

Array(24).fill(0)

注意 fill 仅在 ECMAScript 2015 中引入(别名“6”),因此截至 2017 年浏览器支持仍然非常有限(例如没有 Internet Explorer) .

关于javascript - 默认数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2044760/

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