JavaScript Calculate percentage in array javascript percentage calculation array,javascript calculate percentage array

I have an array like this:

var data = 
    [
       {label:1, quater :'Q1', y:34},
       {label:2, quater:'Q1', y:20},
       {label:3, quater:'Q1', y:30},

       {label:1, quater:'Q2', y:77},
       {label:2, quater:'Q2', y:52},
       {label:3, quater:'Q2', y:3},

       {label:1, quater:'Q3', y:65},
       {label:2, quater:'Q3', y:12},
       {label:3, quater:'Q3', y:9},

       {label:1, quater:'Q4', y:77},
       {label:2, quater:'Q4', y:34},
       {label:3, quater:'Q4', y:5}
    ];

My goal is to add "percent" into each object. Each "percent" is the percentage of the value of y in current object devided by the sum of values of y in quater from Q1 to Q4. For example, the percent in first boject should be 34/(34+77+65+77) The code below is what I tried. It works and calculats the data correctly. I am new to javascript, so I calculate the data step by step which makes the code look long.

I am wondering what is the smarter way to calculate the data to make the code shorter? Thanks!

var quater = ["Q1","Q2","Q3","Q4"];
var sum = 0;
var arr = [];
for(var i = 0; i<quater.length; i++){
  for(var j = 0; j<data.length; j++){
    if (data[j]['quater'] == quater[i])
      sum = sum + data[j]['y'];        
  }
 //console.log(sum)    
 arr.push({
   'quater': quater[i],
   'sum': sum
 })
  sum = 0;
}
var arr1 = [];
var percent;
for(var i = 0; i<data.length;i++){
  for(var j = 0; j<arr.length; j++){
    if(data[i]['quater'] == arr[j]['quater']){
      percent = data[i]['y']/arr[j]['sum'];
    }
  }
  //console.log(percent)
  arr1.push({
    'label': data[i]['label'],
    'y': data[i]['y'],
    'quater': data[i]['quater'],
    'percent': percent
  });
}

console.log(arr1)
Answer:1

Can make a simple object that has quater's as keys and has the sums as values.

Then iterate array again and calculate percentage

var totals={};

data.forEach(function(item){
  totals[item.quater] = (totals[item.quater] || 0 ) + item.y;
});

// totals now looks something like {Q3:18, Q1:16, Q4:66, Q1:12}

data.forEach(function(item){
   item.percent = 100 * item.y / totals[item.quater];
});

Assumes label is unimportant such as representing year. If not concatenate label , delimiter and quater into object keys

EDIT: i may have this backwards and need label's as keys. Approaach is same

Answer:2

You can use forEach or map to this more efficiently. The first map is used to calculate the totals, the second map is used to calculate the percentages. See charlietfl answer, who beat me to the answer, for the forEach method which is pretty much identical.

var data = 
    [
       {label:1, quater :'Q1', y:34},
       {label:2, quater:'Q1', y:20},
       {label:3, quater:'Q1', y:30},

       {label:1, quater:'Q2', y:77},
       {label:2, quater:'Q2', y:52},
       {label:3, quater:'Q2', y:3},

       {label:1, quater:'Q3', y:65},
       {label:2, quater:'Q3', y:12},
       {label:3, quater:'Q3', y:9},

       {label:1, quater:'Q4', y:77},
       {label:2, quater:'Q4', y:34},
       {label:3, quater:'Q4', y:5}
    ];

quartersum = {}

data.map(function(entry) {
  
  quartersum[entry.label] = (quartersum[entry.label]||0) + entry.y
  
})

data.map(function(entry) {
  
  entry.percent = entry.y / quartersum[entry.label] * 100;
  
  document.write(JSON.stringify(entry) + '<br>')
})
Answer:3

I have the HTML code <span class="editableFalse" id="comments"> Lorem ipsum sit dolor amet <br/> Lorem ipsum sit dolor amet </span> <textarea id="textareaComments"></...

I have the HTML code <span class="editableFalse" id="comments"> Lorem ipsum sit dolor amet <br/> Lorem ipsum sit dolor amet </span> <textarea id="textareaComments"></...

  1. read from html file python
  2. read from html file c#
  3. read from html file java
  4. read from html python
  5. read from html
  6. read from html java
  7. read from html file
  8. read from html input javascript
  9. read from html js
  10. read from html page
  11. read html node js
  12. read html from url python
  13. read html from url c#
  14. read html from url javascript
  15. read html from url java
  16. read html from url
  17. read html from javascript
  18. read html from file javascript
  19. read html from webview android
  20. read html from assets android

Can't find the answer anywhere. I don't know how to get a hidden value from a <td> on jquery. <td class='equis' value='8'>X</td> I also tried putting the value='8' on a <span>...

Can't find the answer anywhere. I don't know how to get a hidden value from a <td> on jquery. <td class='equis' value='8'>X</td> I also tried putting the value='8' on a <span>...

  1. hidden value from javascript
  2. hidden value in html
  3. hidden value in mvc
  4. hidden value in php
  5. hidden value in asp.net
  6. hidden value in select option
  7. hidden value in form
  8. hidden value of a man
  9. hidden value in jsp
  10. hidden #value in excel
  11. hidden value of stone age food
  12. hidden value in contact form 7
  13. hidden value in mvc view
  14. hidden value in c#
  15. hidden value in option html
  16. hidden value of landscapes
  17. hidden value of consumer items
  18. hidden value in html table
  19. hidden value in form html
  20. hidden value in td

here is javascript what i have tried function toggle(clicked_id,name) { alert(clicked_id); alert(name); var text = document.getElementById("clicked_id"); var ele = document....

here is javascript what i have tried function toggle(clicked_id,name) { alert(clicked_id); alert(name); var text = document.getElementById("clicked_id"); var ele = document....

I have number of different buttons on my website and I want to assign different task to each button, currently I am referencing them one by one using getElementByID and because I have so many buttons ...

I have number of different buttons on my website and I want to assign different task to each button, currently I am referencing them one by one using getElementByID and because I have so many buttons ...

  1. loop through buttons javascript
  2. loop through buttons vba
  3. loop through buttons jquery
  4. loop through buttons
  5. loop through buttons vb.net
  6. loop through buttons java
  7. loop through buttons android
  8. loop through radio buttons javascript
  9. loop through option buttons vba
  10. loop through radio buttons jquery
  11. loop through command buttons vba
  12. c# loop through buttons on form
  13. loop through all buttons jquery
  14. c# loop through buttons
  15. loop through radio buttons
  16. tkinter loop through buttons
  17. wpf loop through buttons
  18. selenium loop through buttons
  19. loop through radio buttons php
  20. excel loop through buttons