Most efficient way to create a zero filled JavaScript array?(创建零填充 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 数组的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:创建零填充 JavaScript 数组的最有效方法是什么?
基础教程推荐
- fetch 是否支持原生多文件上传? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
