创建零填充 JavaScript 数组的最有效方法? [英] Most efficient way to create a zero filled JavaScript array?

查看:53
本文介绍了创建零填充 JavaScript 数组的最有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 JavaScript 中创建任意长度的零填充数组的最有效方法是什么?

What is the most efficient way to create an arbitrary length zero filled array in JavaScript?

推荐答案

ES6 引入 Array.prototype.fill.可以这样使用:

ES6 introduces Array.prototype.fill. It can be used like this:

new Array(len).fill(0);

不确定它是否很快,但我喜欢它,因为它简短且自我描述.

Not sure if it's fast, but I like it because it's short and self-describing.

它仍然不在 IE 中(检查兼容性),但有一个 polyfill 可用.

It's still not in IE (check compatibility), but there's a polyfill available.

这篇关于创建零填充 JavaScript 数组的最有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆