Skip to content

Instantly share code, notes, and snippets.

@sirdarthvader
Created March 6, 2020 22:54
Show Gist options
  • Save sirdarthvader/35734cd0d514a822856f97a04f3ba652 to your computer and use it in GitHub Desktop.
Save sirdarthvader/35734cd0d514a822856f97a04f3ba652 to your computer and use it in GitHub Desktop.
remove array elements using array.filet and array.reduce
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