emmm… I am studying hard, waiting for autumn recruitment, looking forward to the offer from a big factory. A few days ago, a classmate met a cat eye, which included a question about array flattening, but I am also busy writing algorithm these days, so I sorted it out.

flat

Array flattening is the conversion of a nested array (which can be any number of layers) to an array with only one layer. For example [1, [2, [3, 4]] => [1, 2, 3, 4]

Here are some simple ways to flatten an array

recursive



toString



The toString method is very simple. Turn it into a string, split it into an array, and then convert it to a Number.

Es6 extension operator



  • The reduce method

   

The array reduce method has a callback function pre, which is the result of each callback. The initial value is [], and current is the current element, which is actually the same as method 1

I’m almost done here, EMMM… If you have a better opinion, see you in the comments section