Guest User

Untitled

a guest
Jan 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. // pure functions
  2. const getPercent = (array) => {
  3. const maxValue = Math.max(...array)
  4. return array.map((n) => (n/maxValue) * 100 )
  5. }
  6. // [100, 40, 20]
  7.  
  8. // use
  9. const setPercents = (objects) => {
  10. const values = objects.map(o => o.value)
  11. const percents = getPercent(values)
  12. return objects.map((object, index) => ({...object, percent: percents[index]}) )
  13. }
  14.  
  15. setPercents([ {type: "critic", value: 25}, {type: "medium", value: 10}, {type: "low", value: 5} ])
  16. // [ {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