Solved

Javascript - calculate average from multid array

Posted on 2013-05-09
1
324 Views
Last Modified: 2013-05-09
Hi,

How do I calculate the average of y?

var activity = [{
	name: 'activity',
	data: 
		[
		{name: 'one', color: '#2c2d2f', y: 12.00}, 
		{name: 'two', color: '#189e31', y: 78.4},
		{name: 'three', color: '#c1171d', y: 9.6}
		]
}]

Open in new window


Thanks
0
Comment
Question by:mhdi
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39154061
test page : http://jsfiddle.net/wHbcW/
var activity = [{
	name: 'activity',
	data: 
		[
		{name: 'one', color: '#2c2d2f', y: 12.00}, 
		{name: 'two', color: '#189e31', y: 78.4},
		{name: 'three', color: '#c1171d', y: 9.6}
		]
}];

var total = 0;
var q = activity[0].data.length;
for(var i=0;i<q;i++) total += activity[0].data[i].y;
var avg = total/q;

alert(avg);

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question