假设我有一个如下所示的变量
var arr = [undefined];
如何使用 ES6 Array.fill()
使 arr
等于 []
?
编辑: 我正在做一个 katas 并得到了一个像
这样的测试用例it('fill only changes content, adds no new elements', function() {
const arr = [undefined].fill();
assert.deepEqual(arr, []);
});
不知道如何制作。我可以做到删除未定义。但除此之外还有什么好的方法吗?只是好奇
最佳答案
How to make
[undefined]
equal to[]
using ES6Array.fill()
?
你不能,如fill
不改变数组的长度。
看起来像they really want you to remove the undefined
,或使用其他内容,例如 arr.splice(0, 1);
。
关于javascript - 如何使用 ES6 Array.fill() 清空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32309390/