Sum of array, fetched data from database












0















I used json to send prices over.
How do i sum all the numbers in this array?



 arr[i].itemPrice


I have tried this but i am only getting the second value and not the total



for (var i =0; i<arr.length;i++){
sum += arr[i].itemPrice;
alert(sum);
}









share|improve this question




















  • 1





    Please be careful with how you tag your question. If your question is about javascript, there is little benefit to you in attracting java experts to your question.

    – Joe C
    Nov 25 '18 at 13:06






  • 1





    You can move alert outside the loop

    – Nitish Narang
    Nov 25 '18 at 13:09
















0















I used json to send prices over.
How do i sum all the numbers in this array?



 arr[i].itemPrice


I have tried this but i am only getting the second value and not the total



for (var i =0; i<arr.length;i++){
sum += arr[i].itemPrice;
alert(sum);
}









share|improve this question




















  • 1





    Please be careful with how you tag your question. If your question is about javascript, there is little benefit to you in attracting java experts to your question.

    – Joe C
    Nov 25 '18 at 13:06






  • 1





    You can move alert outside the loop

    – Nitish Narang
    Nov 25 '18 at 13:09














0












0








0








I used json to send prices over.
How do i sum all the numbers in this array?



 arr[i].itemPrice


I have tried this but i am only getting the second value and not the total



for (var i =0; i<arr.length;i++){
sum += arr[i].itemPrice;
alert(sum);
}









share|improve this question
















I used json to send prices over.
How do i sum all the numbers in this array?



 arr[i].itemPrice


I have tried this but i am only getting the second value and not the total



for (var i =0; i<arr.length;i++){
sum += arr[i].itemPrice;
alert(sum);
}






javascript arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 13:06









Joe C

11.7k62543




11.7k62543










asked Nov 25 '18 at 13:01









bobbybobby

367




367








  • 1





    Please be careful with how you tag your question. If your question is about javascript, there is little benefit to you in attracting java experts to your question.

    – Joe C
    Nov 25 '18 at 13:06






  • 1





    You can move alert outside the loop

    – Nitish Narang
    Nov 25 '18 at 13:09














  • 1





    Please be careful with how you tag your question. If your question is about javascript, there is little benefit to you in attracting java experts to your question.

    – Joe C
    Nov 25 '18 at 13:06






  • 1





    You can move alert outside the loop

    – Nitish Narang
    Nov 25 '18 at 13:09








1




1





Please be careful with how you tag your question. If your question is about javascript, there is little benefit to you in attracting java experts to your question.

– Joe C
Nov 25 '18 at 13:06





Please be careful with how you tag your question. If your question is about javascript, there is little benefit to you in attracting java experts to your question.

– Joe C
Nov 25 '18 at 13:06




1




1





You can move alert outside the loop

– Nitish Narang
Nov 25 '18 at 13:09





You can move alert outside the loop

– Nitish Narang
Nov 25 '18 at 13:09












4 Answers
4






active

oldest

votes


















2














You can use the Array's reduce function (ES6):



const sum = arr.reduce((acc, el) => acc + el.itemPrice, 0);


The reduce function allows you to iterate over a collection and accumulate the elements data however you'd like.






share|improve this answer
























  • The numbers i have are 78.00 and 80.00. But it comes out as 078.0080.00, any idea why?

    – bobby
    Nov 25 '18 at 13:12











  • Are the numbers saved as strings? Because it looks like string concatenation

    – Gilad Bar
    Nov 25 '18 at 13:14











  • If it's a string, you can change el.itemPrice to Number(el.itemPrice)

    – Nitish Narang
    Nov 25 '18 at 13:16











  • It works now thank you!

    – bobby
    Nov 25 '18 at 13:18



















1














Use the following :



for (var i = 0; i < arr.length; i++) {
sum += arr[i].itemPrice;
}
alert(sum);


Alert the sum after the loop completes.






share|improve this answer































    1














    Looks like your array is something like below , you can use Array.reduce for this






    let arr = [{ itemPrice: 1}, { itemPrice: 2}, { itemPrice: 3}]

    console.log(arr.reduce((a, {itemPrice}) => a + itemPrice, 0))








    share|improve this answer































      0














      Use Array#reduce






      const data = [{itemPrice:20.00}, {itemPrice:15.00}, {itemPrice:25.00}];

      const total = data.reduce((sum, {itemPrice})=>sum+itemPrice, 0);

      console.log(total);








      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',
        autoActivateHeartbeat: false,
        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%2f53467706%2fsum-of-array-fetched-data-from-database%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        You can use the Array's reduce function (ES6):



        const sum = arr.reduce((acc, el) => acc + el.itemPrice, 0);


        The reduce function allows you to iterate over a collection and accumulate the elements data however you'd like.






        share|improve this answer
























        • The numbers i have are 78.00 and 80.00. But it comes out as 078.0080.00, any idea why?

          – bobby
          Nov 25 '18 at 13:12











        • Are the numbers saved as strings? Because it looks like string concatenation

          – Gilad Bar
          Nov 25 '18 at 13:14











        • If it's a string, you can change el.itemPrice to Number(el.itemPrice)

          – Nitish Narang
          Nov 25 '18 at 13:16











        • It works now thank you!

          – bobby
          Nov 25 '18 at 13:18
















        2














        You can use the Array's reduce function (ES6):



        const sum = arr.reduce((acc, el) => acc + el.itemPrice, 0);


        The reduce function allows you to iterate over a collection and accumulate the elements data however you'd like.






        share|improve this answer
























        • The numbers i have are 78.00 and 80.00. But it comes out as 078.0080.00, any idea why?

          – bobby
          Nov 25 '18 at 13:12











        • Are the numbers saved as strings? Because it looks like string concatenation

          – Gilad Bar
          Nov 25 '18 at 13:14











        • If it's a string, you can change el.itemPrice to Number(el.itemPrice)

          – Nitish Narang
          Nov 25 '18 at 13:16











        • It works now thank you!

          – bobby
          Nov 25 '18 at 13:18














        2












        2








        2







        You can use the Array's reduce function (ES6):



        const sum = arr.reduce((acc, el) => acc + el.itemPrice, 0);


        The reduce function allows you to iterate over a collection and accumulate the elements data however you'd like.






        share|improve this answer













        You can use the Array's reduce function (ES6):



        const sum = arr.reduce((acc, el) => acc + el.itemPrice, 0);


        The reduce function allows you to iterate over a collection and accumulate the elements data however you'd like.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 13:03









        Gilad BarGilad Bar

        753314




        753314













        • The numbers i have are 78.00 and 80.00. But it comes out as 078.0080.00, any idea why?

          – bobby
          Nov 25 '18 at 13:12











        • Are the numbers saved as strings? Because it looks like string concatenation

          – Gilad Bar
          Nov 25 '18 at 13:14











        • If it's a string, you can change el.itemPrice to Number(el.itemPrice)

          – Nitish Narang
          Nov 25 '18 at 13:16











        • It works now thank you!

          – bobby
          Nov 25 '18 at 13:18



















        • The numbers i have are 78.00 and 80.00. But it comes out as 078.0080.00, any idea why?

          – bobby
          Nov 25 '18 at 13:12











        • Are the numbers saved as strings? Because it looks like string concatenation

          – Gilad Bar
          Nov 25 '18 at 13:14











        • If it's a string, you can change el.itemPrice to Number(el.itemPrice)

          – Nitish Narang
          Nov 25 '18 at 13:16











        • It works now thank you!

          – bobby
          Nov 25 '18 at 13:18

















        The numbers i have are 78.00 and 80.00. But it comes out as 078.0080.00, any idea why?

        – bobby
        Nov 25 '18 at 13:12





        The numbers i have are 78.00 and 80.00. But it comes out as 078.0080.00, any idea why?

        – bobby
        Nov 25 '18 at 13:12













        Are the numbers saved as strings? Because it looks like string concatenation

        – Gilad Bar
        Nov 25 '18 at 13:14





        Are the numbers saved as strings? Because it looks like string concatenation

        – Gilad Bar
        Nov 25 '18 at 13:14













        If it's a string, you can change el.itemPrice to Number(el.itemPrice)

        – Nitish Narang
        Nov 25 '18 at 13:16





        If it's a string, you can change el.itemPrice to Number(el.itemPrice)

        – Nitish Narang
        Nov 25 '18 at 13:16













        It works now thank you!

        – bobby
        Nov 25 '18 at 13:18





        It works now thank you!

        – bobby
        Nov 25 '18 at 13:18













        1














        Use the following :



        for (var i = 0; i < arr.length; i++) {
        sum += arr[i].itemPrice;
        }
        alert(sum);


        Alert the sum after the loop completes.






        share|improve this answer




























          1














          Use the following :



          for (var i = 0; i < arr.length; i++) {
          sum += arr[i].itemPrice;
          }
          alert(sum);


          Alert the sum after the loop completes.






          share|improve this answer


























            1












            1








            1







            Use the following :



            for (var i = 0; i < arr.length; i++) {
            sum += arr[i].itemPrice;
            }
            alert(sum);


            Alert the sum after the loop completes.






            share|improve this answer













            Use the following :



            for (var i = 0; i < arr.length; i++) {
            sum += arr[i].itemPrice;
            }
            alert(sum);


            Alert the sum after the loop completes.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 25 '18 at 13:03









            Nicholas KNicholas K

            7,76661537




            7,76661537























                1














                Looks like your array is something like below , you can use Array.reduce for this






                let arr = [{ itemPrice: 1}, { itemPrice: 2}, { itemPrice: 3}]

                console.log(arr.reduce((a, {itemPrice}) => a + itemPrice, 0))








                share|improve this answer




























                  1














                  Looks like your array is something like below , you can use Array.reduce for this






                  let arr = [{ itemPrice: 1}, { itemPrice: 2}, { itemPrice: 3}]

                  console.log(arr.reduce((a, {itemPrice}) => a + itemPrice, 0))








                  share|improve this answer


























                    1












                    1








                    1







                    Looks like your array is something like below , you can use Array.reduce for this






                    let arr = [{ itemPrice: 1}, { itemPrice: 2}, { itemPrice: 3}]

                    console.log(arr.reduce((a, {itemPrice}) => a + itemPrice, 0))








                    share|improve this answer













                    Looks like your array is something like below , you can use Array.reduce for this






                    let arr = [{ itemPrice: 1}, { itemPrice: 2}, { itemPrice: 3}]

                    console.log(arr.reduce((a, {itemPrice}) => a + itemPrice, 0))








                    let arr = [{ itemPrice: 1}, { itemPrice: 2}, { itemPrice: 3}]

                    console.log(arr.reduce((a, {itemPrice}) => a + itemPrice, 0))





                    let arr = [{ itemPrice: 1}, { itemPrice: 2}, { itemPrice: 3}]

                    console.log(arr.reduce((a, {itemPrice}) => a + itemPrice, 0))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 25 '18 at 13:08









                    Nitish NarangNitish Narang

                    2,9601815




                    2,9601815























                        0














                        Use Array#reduce






                        const data = [{itemPrice:20.00}, {itemPrice:15.00}, {itemPrice:25.00}];

                        const total = data.reduce((sum, {itemPrice})=>sum+itemPrice, 0);

                        console.log(total);








                        share|improve this answer




























                          0














                          Use Array#reduce






                          const data = [{itemPrice:20.00}, {itemPrice:15.00}, {itemPrice:25.00}];

                          const total = data.reduce((sum, {itemPrice})=>sum+itemPrice, 0);

                          console.log(total);








                          share|improve this answer


























                            0












                            0








                            0







                            Use Array#reduce






                            const data = [{itemPrice:20.00}, {itemPrice:15.00}, {itemPrice:25.00}];

                            const total = data.reduce((sum, {itemPrice})=>sum+itemPrice, 0);

                            console.log(total);








                            share|improve this answer













                            Use Array#reduce






                            const data = [{itemPrice:20.00}, {itemPrice:15.00}, {itemPrice:25.00}];

                            const total = data.reduce((sum, {itemPrice})=>sum+itemPrice, 0);

                            console.log(total);








                            const data = [{itemPrice:20.00}, {itemPrice:15.00}, {itemPrice:25.00}];

                            const total = data.reduce((sum, {itemPrice})=>sum+itemPrice, 0);

                            console.log(total);





                            const data = [{itemPrice:20.00}, {itemPrice:15.00}, {itemPrice:25.00}];

                            const total = data.reduce((sum, {itemPrice})=>sum+itemPrice, 0);

                            console.log(total);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 25 '18 at 13:10









                            kemicofakemicofa

                            10.7k43984




                            10.7k43984






























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467706%2fsum-of-array-fetched-data-from-database%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

                                To store a contact into the json file from server.js file using a class in NodeJS

                                Redirect URL with Chrome Remote Debugging Android Devices

                                Dieringhausen