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.










share|improve this question




















  • 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















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.










share|improve this question




















  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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








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












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);








share|improve this answer




























    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);








    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      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

























      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);








      share|improve this answer

























        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);








        share|improve this answer























          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);








          share|improve this answer












          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);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 19:51









          Nina Scholz

          172k1384147




          172k1384147
























              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);








              share|improve this answer

























                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);








                share|improve this answer























                  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);








                  share|improve this answer












                  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);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 at 19:51









                  Hodrobond

                  1,3711116




                  1,3711116






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Sphinx de Gizeh

                      Dijon

                      Guerrita