Created
December 15, 2016 14:01
-
-
Save lleaff/04c879414890d0ab013dac5426f841bf to your computer and use it in GitHub Desktop.
Merge consecutive array elements
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 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