Skip to content

Instantly share code, notes, and snippets.

@adyngom
Last active September 26, 2017 17:06
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 adyngom/5c21cc6ab98559032eefa0e1a98cae09 to your computer and use it in GitHub Desktop.
Save adyngom/5c21cc6ab98559032eefa0e1a98cae09 to your computer and use it in GitHub Desktop.
/**
* Write some code, that will flatten an array of arbitrarily nested arrays of integers into a
* flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
**/
/**
* forEach solution
**/
const FlatLinerFE = function(arr) {
let flatline = [];
let flattFE = function(set) {
set.forEach( currVal => Array.isArray(currVal) ? flattFE(currVal) : flatline.push(currVal) );
};
// check if arr is array if not bail out early
Array.isArray(arr) ? flattFE(arr) : flatline = arr;
return flatline;
};
/**
* Map solution
**/
const FlatLinerMap = function(arr) {
let flatline = [];
let flattMap = (set) => {
set.map((a,v,c) => {
Array.isArray(a) ? flattMap(a) : flatline.push(a);
});
};
// check if arr is array if not bail out early
Array.isArray(arr) ? flattMap(arr) : flatline = arr;
return flatline;
};
/**
* Reduce recursive solution
**/
const FlatLinerReduceRecursive = function(arr) {
let flattReduce = set => set.reduce ((newArr, currVal ) => {
return newArr.concat(Array.isArray(currVal) ? flattReduce(currVal) : currVal), []);
}
// check if arr is array if not bail out early
return Array.isArray(arr) ? flattReduce(arr) : arr;
};
/**
* Reduce non-recursive solution
**/
const FlatLinerReduceStraight = function(arr) {
let flattReduce = set => set.reduce ((newArr, currVal ) => {
return newArr.concat(currVal), []);
}
// check if arr is array if not bail out early
return Array.isArray(arr) ? flattReduce(arr) : arr;
};
/**
* toString + reduce solution
**/
const FlattenToString = function(A) {
return A
.toString()
.split(',')
.reduce( (a,c) => {
let i = parseFloat(c);
c = (!Number.isNaN(i)) ? i : c;
a.push(c);
return a;
}, []);
};
/**
* JSON stringify solution
* thanks to: https://stackoverflow.com/users/2907495/r-santosh-reddy
* from this thread: https://stackoverflow.com/a/43541431/965703
**/
const FlattenJSON = function(A) {
let B = JSON.parse(`[${JSON.stringify(A).replace(/\[|]/g,'')}]`);
return B;
}
let single = "hello, paprika";
let arr = [1, [2, 3, [4, [5, [6] ] ] ] ];
console.group("Testing with array");
console.log("forEach:", FlatLinerFE(arr));
console.log("Map:", FlatLinerMap(arr));
console.log("Reduce Recursive:", FlatLinerReduceRecursive(arr));
console.log("Reduce Straight:", FlatLinerReduceStraight(arr));
console.log("Flatten toString + reduce:", FlattenToString(arr));
console.log("Flatten JSON:", FlattenJSON(arr));
console.groupEnd();
console.group("Testing with non array");
console.log("forEach:", FlatLinerFE(single));
console.log("Map:", FlatLinerMap(single));
console.log("Reduce:", FlatLinerReduce(single));
console.log("Reduce Recursive:", FlatLinerReduceRecursive(single));
console.log("Reduce Straight:", FlatLinerReduceStraight(single));
console.log("Flatten toString + reduce:", FlattenToString(single));
console.log("Flatten JSON:", FlattenJSON(single));
console.groupEnd();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment