Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // pure functions
- const getPercent = (array) => {
- const maxValue = Math.max(...array)
- return array.map((n) => (n/maxValue) * 100 )
- }
- // [100, 40, 20]
- // use
- const setPercents = (objects) => {
- const values = objects.map(o => o.value)
- const percents = getPercent(values)
- return objects.map((object, index) => ({...object, percent: percents[index]}) )
- }
- setPercents([ {type: "critic", value: 25}, {type: "medium", value: 10}, {type: "low", value: 5} ])
- // [ {type: "critic", value: 25, percent: 100}, {type: "medium": 10, percent: 40}, {type: "low", value: 5, percent: 20} ]
Add Comment
Please, Sign In to add comment