Skip to content

Instantly share code, notes, and snippets.

Created April 11, 2017 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/2864b0106009e15602716cd46bab983b to your computer and use it in GitHub Desktop.
Save anonymous/2864b0106009e15602716cd46bab983b to your computer and use it in GitHub Desktop.
onst getLast = (arr, def) => (arr[arr.length - 1] || def)
const getLastUser = arr => {
const last = getLast(arr, [])
return getLast(last, {})
}
const pushIntoLast = (arr, value) => {
const last = getLast(arr, [])
last.push(value)
return arr
}
const groupMessagesByUser = messages => {
console.log(messages)
return messages.reduce((acc, current) => {
const prevUser = getLastUser(acc)
if (prevUser.users_id === current.users_id) {
return pushIntoLast(acc, current)
}
return [ ...acc, [ current ] ]
}, [])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment