Skip to content

Instantly share code, notes, and snippets.

@abdullahoguk
Last active January 23, 2022 21:25
Show Gist options
  • Save abdullahoguk/cb0dd2fa736192859dc51a4ead66c8e2 to your computer and use it in GitHub Desktop.
Save abdullahoguk/cb0dd2fa736192859dc51a4ead66c8e2 to your computer and use it in GitHub Desktop.
Split the array into pairs in JavaScript wit Array Reduce
var arr = [1,5,6,7,4,8,5];
arr.reduce((previous, current, index, array)=>{
return index%2==0 ? [...previous, array.slice(index,index+2)] : previous
},[])
//>[[1,5],[6,7],[4,8],[5]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment