Skip to content

Instantly share code, notes, and snippets.

@psenger
Created June 9, 2023 07:46
Show Gist options
  • Save psenger/0f0c1cde1cb7bf7005bb3b3acc455794 to your computer and use it in GitHub Desktop.
Save psenger/0f0c1cde1cb7bf7005bb3b3acc455794 to your computer and use it in GitHub Desktop.
[Greedy Spy on next value] #Array #JavaScript
const pattern = 'abcdefgh';
for (const [currentChar, nextChar] of pattern.split('').reduce((acc,value,idx,arr)=>{const entry = [value]; if ( arr.length > idx + 1 ) entry.push(arr[idx+1]); acc.push(entry); return acc},[])) {
console.log(currentChar,nextChar)
}
/**
a b
b c
c d
d e
e f
f g
g h
h undefined
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment