That is to make this:
[ [\'dog\',\'cat\', [\'chicken\', \'bear\'] ],[\'mouse\',\'horse\'] ]
into:
[\'dog\',\'cat\',\'chicken\',\'
This solution has been working great for me, and i find it particularly easy to follow:
function flattenArray(arr) {
// the new flattened array
var newArr = [];
// recursive function
function flatten(arr, newArr) {
// go through array
for (var i = 0; i < arr.length; i++) {
// if element i of the current array is a non-array value push it
if (Array.isArray(arr[i]) === false) {
newArr.push(arr[i]);
}
// else the element is an array, so unwrap it
else {
flatten(arr[i], newArr);
}
}
}
flatten(arr, newArr);
return newArr;
}