对象不支持属性或方法'fill'

13

我在网上找不到相同的问题。IE 11出现错误:“对象不支持属性或方法fill”。

var arr = new Array(5);
arr.fill(false);

是否有一种方便的方法来填充数组,而不是使用for循环?谢谢。

7个回答

22

我面临相同的问题,不需要添加任何东西。

只需打开 polyfills.ts 文件并取消以下行的注释:

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';

一切都将开始运作。


13

1
这应该是正确的答案。不要纠结于你最喜欢的方法是否可用 - 如果不可用,只需提供一个 polyfill。 - Tad Donaghe

1
你可以使用Array.apply来获取一个所需长度的数组,然后将值映射到它上面。

var a = Array.apply(null, { length: 5 }).map(function () { return false; });
console.log(a);


2
请注意,对于大型数组来说,这种方法速度相对较慢。 - Bergi

1
我遇到了相同的问题,需要在 polyfill.ts 文件中取消注释以下行:

polyfill.ts 文件路径:angular-App=> src=> polyfill.ts

打开此文件并取消注释以下行:

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
// import 'core-js/es6/symbol';
// import 'core-js/es6/object';
// import 'core-js/es6/function';
// import 'core-js/es6/parse-int';
// import 'core-js/es6/parse-float';
// import 'core-js/es6/number';
// import 'core-js/es6/math';
import 'core-js/es6/string';
// import 'core-js/es6/date';
 import 'core-js/es6/array';
// import 'core-js/es6/regexp';
// import 'core-js/es6/map';
// import 'core-js/es6/weak-map';
// import 'core-js/es6/set';

现在它对我来说可行。

0

const arr = Array.from(Array(10), () => 'none')
console.log(arr)
// ['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']


0

fill(); 在IE中不支持。虽然在EDGE中引入了它。我想foreach是完成任务的方便方式。如果发现更多内容,将进行更新。您可以阅读以下内容:


0

将以下脚本添加到您的索引文件中,从CDN下载pollyfill:

<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default,Array.prototype.includes"></script>

这应该可以工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接