Created
March 6, 2020 22:54
-
-
Save sirdarthvader/35734cd0d514a822856f97a04f3ba652 to your computer and use it in GitHub Desktop.
remove array elements using array.filet and array.reduce
This file contains 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 remove = (arr, func) => | |
Array.isArray(arr) | |
? arr.filter(func).reduce((acc, val) => { | |
arr.splice(arr.indexOf(val), 1); | |
return acc.concat(val); | |
}, []) | |
: []; | |
//EXAMPLES | |
remove([1, 2, 3, 4], n => n % 2 === 0); | |
//outputs | |
//[2,4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment