Coding: Flatten an array in Javascript
Flatten Array: Flattening an array means taking an array that may contain nested arrays (i.e., arrays within arrays) and converting it into a single, one-dimensional array. In other words, it involves removing all the nested arrays and combining their elements into a single array.
We will be discussing multiple solutions.
Solution 1: Using Recursion
The code defines a function called FlattenArray
that takes an array as an argument. The goal of the function is to take a nested array and return a flattened version of it. For example, if the input is [1, 2, [3, 4]]
, the function should return [1, 2, 3, 4]
.
The function starts by creating an empty array called, which will be used to store the flattened version of the input array. It then enters a for
a loop that will iterate through each element in the input array.
For each element in the input array, the function checks if the element is itself an array by using the Array.isArray
method. If the element is an array, the function calls itself recursively with the nested array as an argument and uses the concat
method to combine the flattened nested array with the flatArray
one that was created at the beginning of the function.
If the element is not an array, the function simply pushes the element onto the flatArray
.
After all the elements in the input array have been processed, the function returns the flatArray
.
Complete Code:
// Flatten Array
function FlattenArray(arr) {
var flatArray = []
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
flatArray = flatArray.concat(FlattenArray(arr[i]))
} else {
flatArray.push(arr[i])
}
}
return flatArray
}
var arr = [1, 2, 3, 4, [5, 6, [7, 8]], [9]]
console.log(FlattenArray(arr))
Finally, the code defines an example input array called, which contains a mix of integers and nested arrays. The FlattenArray
function is called arr
an argument, and the result is printed to the console. The output should be,[1, 2, 3, 4, 5, 6, 7, 8, 9]
, which is the flattened version of the input array.
Solution 2: Using Reduce Method
function flattenArray(arr) {
return arr.reduce(function(flatArray, current) {
if (Array.isArray(current)) {
return flatArray.concat(flattenArray(current));
} else {
return flatArray.concat(current);
}
}, []);
}
var arr = [1, 2, 3, 4, [5, 6, [7, 8]], [9]];
console.log(flattenArray(arr)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
In this version of the code, the flattenArray
the function takes the same nested array as input. However, instead of using a for
loop to iterate over the array, it uses the reduce
method.
The reduce
the method takes two arguments: a callback function and an initial value for the accumulator. In this case, the accumulator is an empty array that will be used to build up the flattened version of the input array.
The callback function takes two arguments: the current value being processed (which will be each element of the input array in turn), and the current value of the accumulator.
Inside the callback function, we check whether the current value is an array using Array.isArray
. If it is an array, we call flattenArray
recursively to flatten the nested array and then concatenate the flattened array with the accumulator using the concat
method.
If the current value is not an array, we simply concatenate it with the accumulator using concat
.
Finally, the reduce
the method returns the final value of the accumulator, which will be the flattened version of the input array.
With this code, you should get the same output as before: [1, 2, 3, 4, 5, 6, 7, 8, 9]
.
If you’re interested in learning more about coding in Javascript, be sure to follow me for more code snippets and examples. I’ll be sharing valuable information and tips on how to master the language and improve your skills as a developer. So, stay tuned for more updates, and let’s continue learning together.
Thanks for reading.Happy learning 😄