Calculate Average of JSON Values
up vote
0
down vote
favorite
I am trying to calculate the average of JSON values, I tried to add them to an array but not able to. Can someone please help?
My code:
Object.values(data).forEach(d => {
var yo = d.values;
console.log(yo);
});
My results:
9892308
9894522
3298714
3300055
I need to calculate the average of these values. How do I do that?
Code to push values in an array:
Object.values(data).forEach(d => {
var theArray = ;
var yo = d.values;
theArray = theArray.push(yo);
console.log(theArray);
});
Result:
1
I am not able to push the values for me to calculate the array.
javascript jquery json foreach average
|
show 5 more comments
up vote
0
down vote
favorite
I am trying to calculate the average of JSON values, I tried to add them to an array but not able to. Can someone please help?
My code:
Object.values(data).forEach(d => {
var yo = d.values;
console.log(yo);
});
My results:
9892308
9894522
3298714
3300055
I need to calculate the average of these values. How do I do that?
Code to push values in an array:
Object.values(data).forEach(d => {
var theArray = ;
var yo = d.values;
theArray = theArray.push(yo);
console.log(theArray);
});
Result:
1
I am not able to push the values for me to calculate the array.
javascript jquery json foreach average
1
I tried to add them to an array but not able to show us what you tried
– Taplar
Nov 21 at 19:45
Otherwise, conceptually, you'd set a variable to 0 before the forEach, add the values to it in the forEach, and after the forEach divide by the number of values.
– Taplar
Nov 21 at 19:45
You would add them all together & then divide them by the number of items. That's how you calculate the average of a set.
– admcfajn
Nov 21 at 19:47
The math is pretty basic. How manyObject.values()are there? What's the total? How do you add numbers in javscript?
– charlietfl
Nov 21 at 19:47
Also, slight smell. Your model variable is called "values", but it appears to only have a single value in it.
– Taplar
Nov 21 at 19:49
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to calculate the average of JSON values, I tried to add them to an array but not able to. Can someone please help?
My code:
Object.values(data).forEach(d => {
var yo = d.values;
console.log(yo);
});
My results:
9892308
9894522
3298714
3300055
I need to calculate the average of these values. How do I do that?
Code to push values in an array:
Object.values(data).forEach(d => {
var theArray = ;
var yo = d.values;
theArray = theArray.push(yo);
console.log(theArray);
});
Result:
1
I am not able to push the values for me to calculate the array.
javascript jquery json foreach average
I am trying to calculate the average of JSON values, I tried to add them to an array but not able to. Can someone please help?
My code:
Object.values(data).forEach(d => {
var yo = d.values;
console.log(yo);
});
My results:
9892308
9894522
3298714
3300055
I need to calculate the average of these values. How do I do that?
Code to push values in an array:
Object.values(data).forEach(d => {
var theArray = ;
var yo = d.values;
theArray = theArray.push(yo);
console.log(theArray);
});
Result:
1
I am not able to push the values for me to calculate the array.
javascript jquery json foreach average
javascript jquery json foreach average
edited Nov 21 at 19:50
asked Nov 21 at 19:43
newbierp
365
365
1
I tried to add them to an array but not able to show us what you tried
– Taplar
Nov 21 at 19:45
Otherwise, conceptually, you'd set a variable to 0 before the forEach, add the values to it in the forEach, and after the forEach divide by the number of values.
– Taplar
Nov 21 at 19:45
You would add them all together & then divide them by the number of items. That's how you calculate the average of a set.
– admcfajn
Nov 21 at 19:47
The math is pretty basic. How manyObject.values()are there? What's the total? How do you add numbers in javscript?
– charlietfl
Nov 21 at 19:47
Also, slight smell. Your model variable is called "values", but it appears to only have a single value in it.
– Taplar
Nov 21 at 19:49
|
show 5 more comments
1
I tried to add them to an array but not able to show us what you tried
– Taplar
Nov 21 at 19:45
Otherwise, conceptually, you'd set a variable to 0 before the forEach, add the values to it in the forEach, and after the forEach divide by the number of values.
– Taplar
Nov 21 at 19:45
You would add them all together & then divide them by the number of items. That's how you calculate the average of a set.
– admcfajn
Nov 21 at 19:47
The math is pretty basic. How manyObject.values()are there? What's the total? How do you add numbers in javscript?
– charlietfl
Nov 21 at 19:47
Also, slight smell. Your model variable is called "values", but it appears to only have a single value in it.
– Taplar
Nov 21 at 19:49
1
1
I tried to add them to an array but not able to show us what you tried
– Taplar
Nov 21 at 19:45
I tried to add them to an array but not able to show us what you tried
– Taplar
Nov 21 at 19:45
Otherwise, conceptually, you'd set a variable to 0 before the forEach, add the values to it in the forEach, and after the forEach divide by the number of values.
– Taplar
Nov 21 at 19:45
Otherwise, conceptually, you'd set a variable to 0 before the forEach, add the values to it in the forEach, and after the forEach divide by the number of values.
– Taplar
Nov 21 at 19:45
You would add them all together & then divide them by the number of items. That's how you calculate the average of a set.
– admcfajn
Nov 21 at 19:47
You would add them all together & then divide them by the number of items. That's how you calculate the average of a set.
– admcfajn
Nov 21 at 19:47
The math is pretty basic. How many
Object.values() are there? What's the total? How do you add numbers in javscript?– charlietfl
Nov 21 at 19:47
The math is pretty basic. How many
Object.values() are there? What's the total? How do you add numbers in javscript?– charlietfl
Nov 21 at 19:47
Also, slight smell. Your model variable is called "values", but it appears to only have a single value in it.
– Taplar
Nov 21 at 19:49
Also, slight smell. Your model variable is called "values", but it appears to only have a single value in it.
– Taplar
Nov 21 at 19:49
|
show 5 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You could reduce the data by taking the nth (length) of the value and add it to the average in a single loop.
var data = { a: { values: 9892308 }, b: { values: 9894522 }, c: { values: 3298714 }, d: { values: 3300055 } },
average = Object
.values(data)
.reduce((avg, { values }, _, { length }) => avg + values / length, 0);
console.log(average);add a comment |
up vote
0
down vote
Depends how you have your data structured, but an Array.reduce should get you the sum, and divide by the length for the average.
const data = {
a: {
values: 9892308,
},
b: {
values: 9894522,
},
c: {
values: 3298714,
},
something_weird: {
values: 3300055,
}
}
const sum = Object.values(data).reduce((acc, current) => acc + current.values, 0);
const average = sum / Object.values(data).length;
console.log(average);add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You could reduce the data by taking the nth (length) of the value and add it to the average in a single loop.
var data = { a: { values: 9892308 }, b: { values: 9894522 }, c: { values: 3298714 }, d: { values: 3300055 } },
average = Object
.values(data)
.reduce((avg, { values }, _, { length }) => avg + values / length, 0);
console.log(average);add a comment |
up vote
0
down vote
accepted
You could reduce the data by taking the nth (length) of the value and add it to the average in a single loop.
var data = { a: { values: 9892308 }, b: { values: 9894522 }, c: { values: 3298714 }, d: { values: 3300055 } },
average = Object
.values(data)
.reduce((avg, { values }, _, { length }) => avg + values / length, 0);
console.log(average);add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You could reduce the data by taking the nth (length) of the value and add it to the average in a single loop.
var data = { a: { values: 9892308 }, b: { values: 9894522 }, c: { values: 3298714 }, d: { values: 3300055 } },
average = Object
.values(data)
.reduce((avg, { values }, _, { length }) => avg + values / length, 0);
console.log(average);You could reduce the data by taking the nth (length) of the value and add it to the average in a single loop.
var data = { a: { values: 9892308 }, b: { values: 9894522 }, c: { values: 3298714 }, d: { values: 3300055 } },
average = Object
.values(data)
.reduce((avg, { values }, _, { length }) => avg + values / length, 0);
console.log(average);var data = { a: { values: 9892308 }, b: { values: 9894522 }, c: { values: 3298714 }, d: { values: 3300055 } },
average = Object
.values(data)
.reduce((avg, { values }, _, { length }) => avg + values / length, 0);
console.log(average);var data = { a: { values: 9892308 }, b: { values: 9894522 }, c: { values: 3298714 }, d: { values: 3300055 } },
average = Object
.values(data)
.reduce((avg, { values }, _, { length }) => avg + values / length, 0);
console.log(average);answered Nov 21 at 19:51
Nina Scholz
172k1384147
172k1384147
add a comment |
add a comment |
up vote
0
down vote
Depends how you have your data structured, but an Array.reduce should get you the sum, and divide by the length for the average.
const data = {
a: {
values: 9892308,
},
b: {
values: 9894522,
},
c: {
values: 3298714,
},
something_weird: {
values: 3300055,
}
}
const sum = Object.values(data).reduce((acc, current) => acc + current.values, 0);
const average = sum / Object.values(data).length;
console.log(average);add a comment |
up vote
0
down vote
Depends how you have your data structured, but an Array.reduce should get you the sum, and divide by the length for the average.
const data = {
a: {
values: 9892308,
},
b: {
values: 9894522,
},
c: {
values: 3298714,
},
something_weird: {
values: 3300055,
}
}
const sum = Object.values(data).reduce((acc, current) => acc + current.values, 0);
const average = sum / Object.values(data).length;
console.log(average);add a comment |
up vote
0
down vote
up vote
0
down vote
Depends how you have your data structured, but an Array.reduce should get you the sum, and divide by the length for the average.
const data = {
a: {
values: 9892308,
},
b: {
values: 9894522,
},
c: {
values: 3298714,
},
something_weird: {
values: 3300055,
}
}
const sum = Object.values(data).reduce((acc, current) => acc + current.values, 0);
const average = sum / Object.values(data).length;
console.log(average);Depends how you have your data structured, but an Array.reduce should get you the sum, and divide by the length for the average.
const data = {
a: {
values: 9892308,
},
b: {
values: 9894522,
},
c: {
values: 3298714,
},
something_weird: {
values: 3300055,
}
}
const sum = Object.values(data).reduce((acc, current) => acc + current.values, 0);
const average = sum / Object.values(data).length;
console.log(average);const data = {
a: {
values: 9892308,
},
b: {
values: 9894522,
},
c: {
values: 3298714,
},
something_weird: {
values: 3300055,
}
}
const sum = Object.values(data).reduce((acc, current) => acc + current.values, 0);
const average = sum / Object.values(data).length;
console.log(average);const data = {
a: {
values: 9892308,
},
b: {
values: 9894522,
},
c: {
values: 3298714,
},
something_weird: {
values: 3300055,
}
}
const sum = Object.values(data).reduce((acc, current) => acc + current.values, 0);
const average = sum / Object.values(data).length;
console.log(average);answered Nov 21 at 19:51
Hodrobond
1,3711116
1,3711116
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419463%2fcalculate-average-of-json-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I tried to add them to an array but not able to show us what you tried
– Taplar
Nov 21 at 19:45
Otherwise, conceptually, you'd set a variable to 0 before the forEach, add the values to it in the forEach, and after the forEach divide by the number of values.
– Taplar
Nov 21 at 19:45
You would add them all together & then divide them by the number of items. That's how you calculate the average of a set.
– admcfajn
Nov 21 at 19:47
The math is pretty basic. How many
Object.values()are there? What's the total? How do you add numbers in javscript?– charlietfl
Nov 21 at 19:47
Also, slight smell. Your model variable is called "values", but it appears to only have a single value in it.
– Taplar
Nov 21 at 19:49