array.find 不适用于 Babel
array.find doesn't work with Babel
我正在使用 Babel 转译我的 ES2015 代码。但是它不会为数组翻译 find
。以下行抛出错误 TypeError: options.find is not a function
let options = [2,23,4]
options.find(options, x => x < 10)
使用 babel polyfill。
require("babel/polyfill");
[1, 2, 3].find((x) => x >= 2);
// => 2
或者您可以使用回调。 Array.find(arr, callback)
Array.find([ 1, 2, 3 ], (x) => x >= 2);
// => 2
Array.prototype.find
doesn't work in the runtime · Issue #892 · babel/babel
或者如果您已经在使用 ES6 导入
import 'babel/polyfill';
在较新的版本中它是
import 'babel-polyfill'
来源:Babel Docs
如果您只是将 javascript 文件与 Gulp 或 Grunt 连接起来,您可以在 javascript 文件之前添加脚本:node_modules/babel-polyfill/dist/polyfill.js
.
不要忘记安装它:npm i babel-polyfill
。
我正在使用 Babel 转译我的 ES2015 代码。但是它不会为数组翻译 find
。以下行抛出错误 TypeError: options.find is not a function
let options = [2,23,4]
options.find(options, x => x < 10)
使用 babel polyfill。
require("babel/polyfill");
[1, 2, 3].find((x) => x >= 2);
// => 2
或者您可以使用回调。 Array.find(arr, callback)
Array.find([ 1, 2, 3 ], (x) => x >= 2);
// => 2
Array.prototype.find
doesn't work in the runtime · Issue #892 · babel/babel
或者如果您已经在使用 ES6 导入
import 'babel/polyfill';
在较新的版本中它是
import 'babel-polyfill'
来源:Babel Docs
如果您只是将 javascript 文件与 Gulp 或 Grunt 连接起来,您可以在 javascript 文件之前添加脚本:node_modules/babel-polyfill/dist/polyfill.js
.
不要忘记安装它:npm i babel-polyfill
。