Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array.find doesn't work with Babel

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

let options = [2,23,4]
options.find(options, x => x < 10)
like image 209
Hedge Avatar asked Sep 04 '15 15:09

Hedge


3 Answers

Use babel polyfill.

require("babel/polyfill");

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

See: Polyfill · Babel

Or you can use callback. 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

like image 78
Matt - sanemat Avatar answered Nov 12 '22 06:11

Matt - sanemat


In newer versions it's

import 'babel-polyfill'

source: Babel Docs

like image 29
Salman Hasrat Khan Avatar answered Nov 12 '22 05:11

Salman Hasrat Khan


Or if you're using ES6 imports already

import 'babel/polyfill';
like image 3
Mulan Avatar answered Nov 12 '22 06:11

Mulan