I am trying to get one array with a complete string:
['...']
Given:
Array1 = ['...', ['...'], ['...', 2, 'x']]
This should handle any size array*
Define a function, zooInventory, that accepts a multi-dimensional array of
animal facts.
zooInventory should return a new, flat array. Each element in the new array
should be a sentence about each of the animals in the zoo.
What I have currently:
const zooInventory = array => {
let finalArray = [];
for (let i = 0; i < array.length; i++) {
let currentElement = array[i];
if (!(Array.isArray(currentElement))) {
finalArray.push(currentElement);
} else {
zooInventory(currentElement);
// I thought I could push the currentElement back to beginning to handle additional arrays
}
}
return finalArray;
}
Where my heads at:
I thought I could get away with having it push(...array) in the else statement.
My batman belt contains beginner methods... slice, splice, spread.
What method would be best practice?
Simply use the .flat() method as shown in the snippet below.
More information here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
let arrayOne = ['...', ['...'], ['...', 2, 'x']];
console.log(arrayOne.flat());