Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Flatten any combination of nested ints/arrays
- * in an array recursively into a flat array
- * @param input {Array}
- * @returns {Array}
- */
- function flatten(input) {
- let output = [];
- for(let i = 0; i < input.length; i++) {
- const item = input[i];
- // recursively add arrays
- if(typeof item == 'object' && Array.isArray(item) && item.length) {
- output.push(...flatten(item));
- }
- // add simple number instances
- if(typeof item == 'number') {
- output.push(item);
- }
- }
- return output;
- }
- module.exports = flatten;
Add Comment
Please, Sign In to add comment