Array.from TypeError: 0 is not a function

Viewed 5105

I'm getting a strange error when trying to pass Array.from to Array.prototype.map.

let fn = Array.from.bind(Array); // [Function: bound from]

fn('test') // [ 't', 'e', 's', 't' ]

['test'].map(s => fn(s)) // [ [ 't', 'e', 's', 't' ] ]

['test'].map(fn) // TypeError: 0 is not a function

Full Error:

TypeError: 0 is not a function
    at Function.from (native)
    at Array.map (native)
    at repl:1:10
    at REPLServer.defaultEval (repl.js:260:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:429:12)
    at emitOne (events.js:95:20)
    at REPLServer.emit (events.js:182:7)
    at REPLServer.Interface._onLine (readline.js:211:10)

What's going on?

2 Answers

you can use lodash unary

import * as lodash from 'https://cdn.jsdelivr.net/npm/lodash-es/lodash.min.js'
['ab', 'cd'].map(lodash.unary(Array.from))
Related