Last active
April 7, 2016 19:19
-
-
Save netologist/e0313ac328bf80bec81b7b6814188a76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const items = [1,52,14,54,[10,1,3,5],14,6]; | |
const flatten = (arr) => { | |
const flat = [].concat(...arr) | |
return flat.some(Array.isArray) ? flatten(flat) : flat; | |
} | |
const splitAt = (arr, predicate) => { | |
var selected = []; | |
var others = []; | |
for (var i = 0; i < arr.length; i++) { | |
(predicate(arr[i]) ? selected : others).push(arr[i]); | |
} | |
return [selected, others]; | |
}; | |
console.log(flatten(splitAt(items, x => x === 14))); | |
// [14,14,1,52,54,10,1,3,5,6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment