Skip to content

Instantly share code, notes, and snippets.

@FelipeBarrosCruz
Last active November 23, 2020 04:09
Show Gist options
  • Save FelipeBarrosCruz/9674cac7feedf6436cc3b7fd651017d8 to your computer and use it in GitHub Desktop.
Save FelipeBarrosCruz/9674cac7feedf6436cc3b7fd651017d8 to your computer and use it in GitHub Desktop.
const nameKey = 'numero'
const quantityKey = 'qtd'
const product = [1, 1, 2, 2, 2, 3, 4, 5, 5]
const reduced = product.reduce((acc, num) => ({
...acc,
[num.toString()]: ((acc && acc[num] || 0) + 1)
}), {})
const formated = Object.keys(reduced).map((num) => ({
[nameKey]: num,
[quantityKey]: reduced[num]
}))
// ref: https://www.facebook.com/groups/877782458986503/permalink/3405796499518407
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment