Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE array.flat() Object doesn't support property or method 'flat'

When accessing my website from google chrome everything works fine(also on mobile). but when trying to access from edge \ mobile normal browser(not google chrome) i get

TypeError: Object doesn't support property or method 'flat'

trying to access a function .flat of array.

turns out that it dosent exist on the proto at all. what can i do with it? the childs array is defined as

`let childs = [];`

(using react for front end)enter image description here

like image 379
Almog Hazan Avatar asked Oct 27 '18 13:10

Almog Hazan


People also ask

How do you flatten an array of objects?

You use the flat() method for concatenating sub-arrays recursively into a single array. The flat() method takes a depth value as its parameter which is optional depending on the depth of the array you wish to flatten (concatenate). The flat() method takes in 1 as a depth by default.

What does javascript flat () do?

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

What does flatMap do in typescript?

The flatMap() method returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level.


1 Answers

IE does not support Array.prototype.flat(). you can use reduce and concat as a workaround:

childs = childs.reduce((acc, val) => acc.concat(val), [])
like image 113
wang Avatar answered Nov 15 '22 15:11

wang