Skip to content

Instantly share code, notes, and snippets.

@sktwentysix
Created June 9, 2022 12:03
Show Gist options
  • Save sktwentysix/e731f448feacca12b4cc72250cda3f52 to your computer and use it in GitHub Desktop.
Save sktwentysix/e731f448feacca12b4cc72250cda3f52 to your computer and use it in GitHub Desktop.
A simple array reducer example using objects
const array = [
{ id: 111, framework: 'react' },
{ id: 222, framework: 'angular' },
{ id: 333, framework: 'vue' }
];
const reducerCallback = (prevItem, currItem) => {
return {
id: prevItem.id + currItem.id,
framework: prevItem.framework + "-" + currItem.framework
}
}
console.log(array.reduce(reducerCallback)) // returns `{ id: 666, framework: 'react-angular-vue' }`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment