Solved

Javascript - calculate average from multid array

Posted on 2013-05-09
1
319 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now