-
-
Save leobalter/df3dd9032c04ff4a84a1cc52748cb4e6 to your computer and use it in GitHub Desktop.
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
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