javascript array.find 不适用于 Babel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32401513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:12:41  来源:igfitidea点击:

array.find doesn't work with Babel

javascriptecmascript-6babeljs

提问by Hedge

I'm transpiling my ES2015 code using Babel. However it doesn't translate findfor Arrays. The following line throws the error TypeError: options.find is not a function

我正在使用 Babel 转译我的 ES2015 代码。但是它不会转换find为数组。以下行抛出错误TypeError: options.find is not a function

let options = [2,23,4]
options.find(options, x => x < 10)

回答by Matt - sanemat

Use babel polyfill.

使用 babel polyfill。

require("babel/polyfill");

[1, 2, 3].find((x) => x >= 2);
// => 2

See: Polyfill · Babel

参见:Polyfill · Babel

Or you can use callback. Array.find(arr, callback)

或者你可以使用回调。 Array.find(arr, callback)

Array.find([ 1, 2, 3 ], (x) => x >= 2);
// => 2

Array.prototype.finddoesn't work in the runtime · Issue #892 · babel/babel

Array.prototype.find在运行时不起作用 · 问题 #892 · babel/babel

回答by Salman Hasrat Khan

In newer versions it's

在较新的版本中它是

import 'babel-polyfill'

source: Babel Docs

来源:Babel 文档

回答by Thank you

Or if you're using ES6 imports already

或者如果您已经在使用 ES6 导入

import 'babel/polyfill';

回答by Dmytro Krekota

If you just concatenate your javascript files with Gulp or Grunt, you can add the script before your javascript files: node_modules/babel-polyfill/dist/polyfill.js.

如果你只是用 Gulp 或 Grunt 连接你的 javascript 文件,你可以在你的 javascript 文件之前添加脚本:node_modules/babel-polyfill/dist/polyfill.js.

Do not forget to install it: npm i babel-polyfill.

不要忘记安装它:npm i babel-polyfill