2018-10-04 14:47:55 +01:00
---
title: Steamroller
---
2019-07-24 00:59:27 -07:00
# Steamroller
---
## Problem Explanation
2018-10-04 14:47:55 +01:00
This problem seems simple but you need to make sure to flatten any array, regardless of the level which is what adds a bit of difficulty to the problem.
#### Relevant Links
* < a href = 'http://forum.freecodecamp.com/t/javascript-array-isarray/14284' target = '_blank' rel = 'nofollow' > Array.isArray()</ a >
2019-07-24 00:59:27 -07:00
---
## Hints
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
### Hint 1
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
You need to check if an element is an array or not.
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
### Hint 2
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
If you are dealing with an array, then you need flatten it by getting the value inside of the array. This means if you have `[[4]]` then instead of returning `[4]` you need to return `4` . If you get `[[[4]]]` then the same, you want the `4` . You can access it with `arr[index1][index2]` to go a level deeper.
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
### Hint 3
2018-10-04 14:47:55 +01:00
You will definitely need recursion or another way to go beyond two level arrays to make the code flexible and not hard-coded to the answers needed. Have fun!
2019-07-24 00:59:27 -07:00
---
## Solutions
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
```javascript
function steamrollArray(arr) {
var flattenedArray = [];
// Create function that adds an element if it is not an array.
// If it is an array, then loops through it and uses recursion on that array.
var flatten = function(arg) {
if (!Array.isArray(arg)) {
flattenedArray.push(arg);
} else {
for (var a in arg) {
flatten(arg[a]);
}
2018-10-04 14:47:55 +01:00
}
2019-07-24 00:59:27 -07:00
};
// Call the function for each element in the array
arr.forEach(flatten);
return flattenedArray;
}
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
// test here
steamrollArray([1, [2], [3, [[4]]]]);
```
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-04 14:47:55 +01:00
* Create a new variable to keep flattened arrays.
2019-06-07 22:48:08 -07:00
* Create a function that will add non-array elements to the new variable, and for the ones that are array, loop through them to get the element.
* It does that by using recursion, if the element is an array then call the function again with a layer of array deeper to check if it is an array or not. If it is not then push that non-array element to the variable that gets returned. Otherwise, keep going deeper.
* Invoke the function, the first time you will always pass it an array, so it always falls into the isArray branch
2018-10-04 14:47:55 +01:00
* Return the flattened array.
#### Relevant Links
* < a href = 'http://forum.freecodecamp.com/t/javascript-array-prototype-push/14298' target = '_blank' rel = 'nofollow' > Array.push()</ a >
* < a href = 'http://forum.freecodecamp.com/t/javascript-array-prototype-foreach/14290' target = '_blank' rel = 'nofollow' > Array.forEach()</ a >
2019-07-24 00:59:27 -07:00
< / details >
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 2 (Click to Show/Hide)< / summary >
```javascript
function steamrollArray(arr) {
let flat = [].concat(...arr);
return flat.some(Array.isArray) ? steamrollArray(flat) : flat;
}
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
steamrollArray([1, [2], [3, [[4]]]]);
```
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-04 14:47:55 +01:00
* Use spread operator to concatenate each element of `arr` with an empty array
* Use `Array.some()` method to find out if the new array contains an array still
2019-06-07 22:48:08 -07:00
* If it does, use recursion to call `steamrollArray` again, passing in the new array to repeat the process on the arrays that were deeply nested
2018-10-04 14:47:55 +01:00
* If it does not, return the flattened array
#### Relevant Links
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some' target = '_blank' rel = 'nofollow' > Array.some</ a >
* < a href = 'http://forum.freecodecamp.com/t/javascript-array-prototype-concat/14286' target = '_blank' rel = 'nofollow' > Array.concat</ a >
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator' target = '_blank' rel = 'nofollow' > Spread operator</ a >
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator' target = '_blank' rel = 'nofollow' > Ternary Operator (`condition ? a : b` )</ a >
2019-07-24 00:59:27 -07:00
< / details >
2018-10-19 18:03:25 +03:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 3 (Click to Show/Hide)< / summary >
```javascript
function steamrollArray(arr) {
while (arr.some(element => Array.isArray(element))) {
arr = arr.flat();
}
return arr;
}
2018-10-19 18:03:25 +03:00
2019-07-24 00:59:27 -07:00
steamrollArray([1, [2], [3, [[4]]]]);
```
2018-10-19 18:03:25 +03:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-19 18:03:25 +03:00
* Use `Array.some()` method to find out if the new array contains an array still, if it does flatten the array
2019-06-07 22:48:08 -07:00
* Repeats until there are no more arrays inside arr.
2018-10-19 18:03:25 +03:00
#### Relevant Links
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some' target = '_blank' rel = 'nofollow' > Array.some</ a >
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat' target = '_blank' rel = 'nofollow' > Array.flat</ a >
2019-07-24 00:59:27 -07:00
< / details >
< details > < summary > Solution 4 (Click to Show/Hide)< / summary >
```javascript
function steamrollArray(arr) {
return arr
.toString()
.replace(",,", ",") // "1,2,,3" => "1,2,3"
.split(",") // ['1','2','3']
.map(function(v) {
if (v == "[object Object]") {
// bring back empty objects
return {};
} else if (isNaN(v)) {
// if not a number (string)
return v;
} else {
return parseInt(v); // if a number in a string, convert it
}
});
}
```
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-04 14:47:55 +01:00
2019-06-07 22:48:08 -07:00
* First we turn the array to a string, which will give us a string of numbers separated by a comma, double comma if there was an empty array and literal object text if there was an object, which we can fix later in our if statement.
2018-10-04 14:47:55 +01:00
* We replace the double comma with one, then split it back into an array.
* map through the array and fix object values and convert string numbers to regular numbers.
2019-07-24 00:59:27 -07:00
< / details >
2019-03-21 13:54:12 +04:00
2019-07-24 00:59:27 -07:00
```javascript
const steamrollArray = arr => arr.flat(Infinity);
```
2019-03-21 13:54:12 +04:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2019-03-21 13:54:12 +04:00
* Use `Array.flat()` to flatten an array with `Infinity` as a parameter for the depth.