I'm trying to concat an array of arrays using reduce and I figured that I could use the Array.prototype.concat function like this:
arr = [[1],[2],[3]]
arr.reduce((a, b) => Array.prototype.concat(a, b), [])
Which works fine and gives me the array [1, 2, 3]
. Then I thought I could be even smarter and do it like this:
arr = [[1],[2],[3]]
arr.reduce(Array.prototype.concat, [])
This however gives me an error:
TypeError: Array.prototype.concat called on null or undefined
at Array.reduce (native)
at Object.<anonymous> (/home/axel/Developer/temp/reduce2.js:2:5)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
It seems to think that Array.prototype.concat
is undefined
. Why is this?