Add columns value in react.js

Viewed 559

What is the best approach to add all the columns value?
json

[
  {
    "id": "men",
    "label": "men",
    "value": 3,
    "color": "#468df3"
  },
  {
    "id": "women",
    "label": "women",
    "value": 5,
    "color": "#ba72ff"
  },
  {
    "id": "children",
    "label": "children",
    "value": 5,
    "color": "#a1cfff"
  }
]

I am fetching data from the server I like to add all the values and display it in the console. For example const value = 3+5+5 = 13 in console.

1 Answers

That's not related to reactjs, simply

let raw = '[{"id": "men","label": "men","value": 3,"color": "#468df3"},{"id": "women","label": "women","value": 5,"color": "#ba72ff"},{"id": "children","label": "children","value": 5,"color": "#a1cfff"}]';

let data = JSON.parse(raw);
let sum_value = data.reduce((sum, current)=>{
    return sum + current.value
}, 0);

console.log(sum_value);

Related