To start off with this the first thing is to just start playing around with some basic examples of the array flat method
Here I am just flattening down a single array of arrays just one level.
A depth argument can be given to the array flat method, that will set the number of nested levels to go with it comes to flattening an array. By default the depth is 1, and a depth of 0 can be set which would result in no change of the array.
For the most part I should be able to just use the array flat method and move on with my life when it comes to this sort of thing. However if I am using an old version of node that does not support this method, then I might need some kind of alternative. Also when it comes to coding purely for the sake of learning, making a method like this might not be a waste of time because it can prove to be a good exercise of writing a recurse method when it comes to creating a depth feature for the method.
Many the alternative methods that I see on the web make use of the array reduce and array concat methods. The array reduce method will work on browsers as old as Internet explorer 9, and the array concat method goes back even farther than that. These days I can not say that I would every need or want to go back father than that, so in most cases a method that make use of those array methods should work just fine.
So far so good when it comes to making an alternative to the array flat method. However I will often want to flatten an array more than one level at some times. By default the array flat method has a depth of 1, but I can set the depth higher than that when it comes to having more than one level of nested arrays. So in this example I have made a slightly more advanced example of the basic array fill alternative that I started out with that will call the flatten method itself inside the body of the reducer function.
There is just one more feature that I would want to have in an array flat alliterative and that is to have a depth argument that works just like that of the depth argument in the native method. This is where things can some times get a little involved, but it is good to get into knowing how to make these kinds of methods because they will come up now and then.
In some cases I might want to polly fill the array flat method, for this I came up with a quick solution that is just a slightly revised revision of the stand alone alternative that I made. When it comes to making a polly fill I just need to assign my own method to the name of the method in the prototype object of the Array Object. In these kinds of methods the this keyword will refer to the instance of the array that the method is called off of. Also the method should correctly reproduce the behaviors of the native method when it comes to things like mutating the source array in place or not. The native array flat does not seem to mutate in place, so a polly fill method should do the same.
When it comes to the popular utility library there is also a lodash flatten method that would be yet another example of an alternative to the array flat method.