Skip to content

Instantly share code, notes, and snippets.

@lleaff
Created December 15, 2016 14:01
Show Gist options
  • Save lleaff/04c879414890d0ab013dac5426f841bf to your computer and use it in GitHub Desktop.
Save lleaff/04c879414890d0ab013dac5426f841bf to your computer and use it in GitHub Desktop.
Merge consecutive array elements
const last = a => a[a.length - 1];
const mergeConsecutive = (arr, el) => (arr.length <= 1) ?
arr :
arr.slice(1).reduce((p, v) => v === el && last(p) === v ?
p :
[...p, v], [arr[0]]);
const mergeConsecutiveElements = arr => (arr.length <= 1) ?
arr :
arr.slice(1).reduce((p, v) => last(p) === v ? p : [...p, v], [arr[0]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment