Last active
November 23, 2020 04:09
-
-
Save FelipeBarrosCruz/9674cac7feedf6436cc3b7fd651017d8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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