Skip to content

Instantly share code, notes, and snippets.

@erperejildo
Created January 21, 2020 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erperejildo/643515971a931c05f81173ab017e8a97 to your computer and use it in GitHub Desktop.
Save erperejildo/643515971a931c05f81173ab017e8a97 to your computer and use it in GitHub Desktop.
// First try I was detecting if it was an array or not and create some loops
// if so, but I thought on an easier way to avoid some recursivity
const fixArray = function(array) {
const arrayString = array.toLocaleString(); // "1,2,3..."
const fixedArray = arrayString.split(',').map(function(item) {
return parseInt(item, 10);
});
console.log(fixedArray);
return fixedArray;
}
const nestedArrays1 = [[1,2,[3]],4];
const nestedArrays2 = [[1,2,[3]],4,[[[5]]]];
const nestedArrays3 = [1,2,[3,4,5]];
const fixedArray1 = fixArray(nestedArrays1);
const fixedArray2 = fixArray(nestedArrays2);
const fixedArray3 = fixArray(nestedArrays3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment