2

How to iterate through an array of JSON data. Can you help with the code:

var percent=(100 * this.contributed) / this.max;
percent= Math.floor(percent);

This is the logic. I need to apply this for all objects using for each

var dashboardval= [
    {"contributed": 20, "max": 35 },
    {"contributed": 22, "max": 35},
    {'contributed': 35, "max": 35,},
    {"contributed": 32, "max": 35}
   ];
    
Theo
  • 57,719
  • 8
  • 24
  • 41
sagar kn
  • 21
  • 3

1 Answers1

0

Using ES6 you can do this by reducing the array of objects:

var objects = [{"contributed": 20, "max": 35}, {"contributed": 22, "max": 35}, {'contributed': 35, "max": 35}, {"contributed": 32, "max": 35}];

var percent = objects.reduce((acc, {contributed, max}) => ~~((100 * contributed) / max));

console.log(percent);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64