This is a post on the _.flatten method in lodash, than can be used to flatten nested arrays down into a single array. The flatten method is one of the many array methods in lodash many of which are just wrappers of native methods, but thus is one method where that is not the case.
So for a basic example of _.flatten and the corresponding _.chunk method I will start off with an array of arrays where each array is a number between 1 and 9, and is broken into three arrays of three numbers each. The _.flatten method can be used to flatten such an array of arrays into a single linear array, and then the _.chunk method can be used to break it back down again into the array of arrays. So the chunk and flatten methods are two lodash methods that are often used together in a project to quickly change between an array of arrays form, and just a single array.
So If I make an array of arrays with the array literal notation I can flatten it down into a straight linear array with the lodash _.flatten method, but just passing the array of arrays as the first argument.
Simple enough right, now if I want to break it back down again I will want to use _.chunk.
So then if I have a linear array but then want to break it down into an array of arrays there is the _.chunk method that can do the opposite of /_.flatten. It works by passing the linear array as the first argument, and then the element length of the arrays as the second argument.
If I have a situation in which I am dealing with many nested levels of arrays I can use the _.flattenDepth method that is just like _.flatten only it accepts a second argument that sets the depth at which flattening is to take. With the normal _.flatten method only a single level will be flattened, and with the _.flattenDeep method all levels will be flattened, but with _.flattenDepth I can control the depth of this.
So an example that involves some fun with a gird is in order for a method that has to do with multi dimensional arrays, so lets get into that for a moment as a way to learn why methods like _.flatten come in handy. Say you are making a game that involves to one extent or another a grid, and each grid position has an object associated with it. This gird might be used in a lot of different ways but so far you just have a single minimal module that uses _.chunk to break down a linear array into an array of arrays that each have an object for each x, y position.
There is only a few properties of this modules that store the current state of the grid, and a single public method that can be used to generate the grid. When calling the genMap method it will create an object for each position with basic info like x,y, and the index when it does exist as a linear array. The genMap object also allows for a forPos methods that is called for each position which can be used to make custom properties for the position object.
So here is the code of the module that I was explaining. In it I create a public api, and return that api after calling the genMap method once with default values to make sure that the gird always has some kind of set of values.
The genMap method works by just creating a linear array of objects for each position, and then I use the _.chunk method to break it down into the proper array of arrays form.
So now that I have my grid module it is time to start playing around with some custom maps, and methods that involve the use of _.flatten among other useful methods like _.reduce. How about a map that holds random amounts of money, and I must make a method that tabulates the total sum of the money in the grid.
Here my tab money method works by making use of _.flatten to flatten the grid into a linear array to which I am then using with _.reduce to tabulate the amount of money for each position object.
I hope you enjoyed this post on the lodash _.flatten method, if you think there should be something more to add to this post be sure to let me know in the comments. Also if you enjoyed this post you might want to check out my many other posts on lodash. Some other posts of interest that are relevant to _.flatten might be my post on _.chunk, and _.zip