add up numbers in array












0















I've been following this post but I cannot get my array to add up all numbers in my array.



I use this:



 var array_new = [$(".rightcell.emphasize").text().split('€')];


to give me this array:



array_new: ,102.80,192.60,22.16


then I do this:



var sum = array_new.reduce((a, b) => a + b, 0);
console.log(sum); //gives me this 0,102.80,192.60,22.16


When all I want to do is add up the numbers, I get this result 0,102.80,192.60,22.16. Can anyone advise me?










share|improve this question























  • You are creating an array within an array by putting square brackets around your assignment of array_new. Try this: var array_new = $(".rightcell.emphasize").text().split('€');

    – Patrick Hund
    Nov 27 '18 at 15:43











  • Show us the relevant text in $(".rightcell.emphasize")

    – charlietfl
    Nov 27 '18 at 15:52
















0















I've been following this post but I cannot get my array to add up all numbers in my array.



I use this:



 var array_new = [$(".rightcell.emphasize").text().split('€')];


to give me this array:



array_new: ,102.80,192.60,22.16


then I do this:



var sum = array_new.reduce((a, b) => a + b, 0);
console.log(sum); //gives me this 0,102.80,192.60,22.16


When all I want to do is add up the numbers, I get this result 0,102.80,192.60,22.16. Can anyone advise me?










share|improve this question























  • You are creating an array within an array by putting square brackets around your assignment of array_new. Try this: var array_new = $(".rightcell.emphasize").text().split('€');

    – Patrick Hund
    Nov 27 '18 at 15:43











  • Show us the relevant text in $(".rightcell.emphasize")

    – charlietfl
    Nov 27 '18 at 15:52














0












0








0








I've been following this post but I cannot get my array to add up all numbers in my array.



I use this:



 var array_new = [$(".rightcell.emphasize").text().split('€')];


to give me this array:



array_new: ,102.80,192.60,22.16


then I do this:



var sum = array_new.reduce((a, b) => a + b, 0);
console.log(sum); //gives me this 0,102.80,192.60,22.16


When all I want to do is add up the numbers, I get this result 0,102.80,192.60,22.16. Can anyone advise me?










share|improve this question














I've been following this post but I cannot get my array to add up all numbers in my array.



I use this:



 var array_new = [$(".rightcell.emphasize").text().split('€')];


to give me this array:



array_new: ,102.80,192.60,22.16


then I do this:



var sum = array_new.reduce((a, b) => a + b, 0);
console.log(sum); //gives me this 0,102.80,192.60,22.16


When all I want to do is add up the numbers, I get this result 0,102.80,192.60,22.16. Can anyone advise me?







javascript arrays reduce






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 15:41









artworkjpmartworkjpm

88214




88214













  • You are creating an array within an array by putting square brackets around your assignment of array_new. Try this: var array_new = $(".rightcell.emphasize").text().split('€');

    – Patrick Hund
    Nov 27 '18 at 15:43











  • Show us the relevant text in $(".rightcell.emphasize")

    – charlietfl
    Nov 27 '18 at 15:52



















  • You are creating an array within an array by putting square brackets around your assignment of array_new. Try this: var array_new = $(".rightcell.emphasize").text().split('€');

    – Patrick Hund
    Nov 27 '18 at 15:43











  • Show us the relevant text in $(".rightcell.emphasize")

    – charlietfl
    Nov 27 '18 at 15:52

















You are creating an array within an array by putting square brackets around your assignment of array_new. Try this: var array_new = $(".rightcell.emphasize").text().split('€');

– Patrick Hund
Nov 27 '18 at 15:43





You are creating an array within an array by putting square brackets around your assignment of array_new. Try this: var array_new = $(".rightcell.emphasize").text().split('€');

– Patrick Hund
Nov 27 '18 at 15:43













Show us the relevant text in $(".rightcell.emphasize")

– charlietfl
Nov 27 '18 at 15:52





Show us the relevant text in $(".rightcell.emphasize")

– charlietfl
Nov 27 '18 at 15:52












4 Answers
4






active

oldest

votes


















1














Since your array is composed of undefined and a bunch of strings you have to parse the values to get the numbers. The answer would be:






var data = [,'102.80','192.60','22.16'];

console.log(data.reduce((r,c) => r + parseFloat(c), 0))





However if you do not want to deal with the parsing in that function you can make sure that your array comes out as array of numbers like this:



Array.from([$(".rightcell.emphasize").text().split('€')], (x) => parseFloat(x || 0))


Which would get your array ready for summation and without the need to parse inside the Array.reduce. So it would be something like this:






var strings = [,'102.80','192.60','22.16'];
var numbers = Array.from(strings, (x) => parseFloat(x || 0))

console.log(numbers.reduce((r,c) => r + c, 0))





But in your case it would be shorter since you would do the first 2 lines as one as shown in the 2nd code snippet.






share|improve this answer


























  • you da man, well done sir

    – artworkjpm
    Nov 28 '18 at 9:08



















0














Your elements in the array are treated as strings, therefore the + in your reduce function concatenates the array elements into another string. Try to use parseFloat() to treat them as numbers:



var sum = array_new.reduce((a, b) => a + parseFloat(b), 0);


EDIT: Thanks to charlietfl who mentioned, that parseFloat(a) is redundant, fixed it.



EDIT: (since you already deleted your comment, here would be the solution for your problem): in your case this solution won't work, because one of your array elements is "" (an empty string) which can't be treated as a number, so you could map your array values before you try to add them up:



array_new.map(a => (a == "") ? "0" : a);





share|improve this answer


























  • parseFloat(a) is redundant since accumulator is number

    – charlietfl
    Nov 27 '18 at 15:50



















0














That's because your original array is a string array.



Use parseFloat to create numbers from the strings.



var strArr = ['102.80','192.60','22.16'];
var sum = strArr.reduce((a,b) => a + parseFloat(b),0); // 317.56





share|improve this answer


























  • Why create a new array with map()?

    – charlietfl
    Nov 27 '18 at 15:48











  • just for the example..if your into optimizations then you should use for instead of reduce.

    – Amir Popovich
    Nov 27 '18 at 15:52











  • Also the outer shouldn't be there

    – charlietfl
    Nov 27 '18 at 15:54



















0














in javascript, you can concat numbers by + or strings as well .
I will give you an example :



var x = "2" + "1"; // x = 21
var x = 2 + 1 ; // x = 3


You can look to this.



You should convert your array to integers (parseInt) or floats(parseFloat)
in this function array_new.reduce((a, b) => a + b, 0);
It will be something like



array_new.reduce((a, b) => a + parseInt(b), 0);//or
array_new.reduce((a, b) => a + parseFloat(b), 0);





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%2f53503167%2fadd-up-numbers-in-array%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









    1














    Since your array is composed of undefined and a bunch of strings you have to parse the values to get the numbers. The answer would be:






    var data = [,'102.80','192.60','22.16'];

    console.log(data.reduce((r,c) => r + parseFloat(c), 0))





    However if you do not want to deal with the parsing in that function you can make sure that your array comes out as array of numbers like this:



    Array.from([$(".rightcell.emphasize").text().split('€')], (x) => parseFloat(x || 0))


    Which would get your array ready for summation and without the need to parse inside the Array.reduce. So it would be something like this:






    var strings = [,'102.80','192.60','22.16'];
    var numbers = Array.from(strings, (x) => parseFloat(x || 0))

    console.log(numbers.reduce((r,c) => r + c, 0))





    But in your case it would be shorter since you would do the first 2 lines as one as shown in the 2nd code snippet.






    share|improve this answer


























    • you da man, well done sir

      – artworkjpm
      Nov 28 '18 at 9:08
















    1














    Since your array is composed of undefined and a bunch of strings you have to parse the values to get the numbers. The answer would be:






    var data = [,'102.80','192.60','22.16'];

    console.log(data.reduce((r,c) => r + parseFloat(c), 0))





    However if you do not want to deal with the parsing in that function you can make sure that your array comes out as array of numbers like this:



    Array.from([$(".rightcell.emphasize").text().split('€')], (x) => parseFloat(x || 0))


    Which would get your array ready for summation and without the need to parse inside the Array.reduce. So it would be something like this:






    var strings = [,'102.80','192.60','22.16'];
    var numbers = Array.from(strings, (x) => parseFloat(x || 0))

    console.log(numbers.reduce((r,c) => r + c, 0))





    But in your case it would be shorter since you would do the first 2 lines as one as shown in the 2nd code snippet.






    share|improve this answer


























    • you da man, well done sir

      – artworkjpm
      Nov 28 '18 at 9:08














    1












    1








    1







    Since your array is composed of undefined and a bunch of strings you have to parse the values to get the numbers. The answer would be:






    var data = [,'102.80','192.60','22.16'];

    console.log(data.reduce((r,c) => r + parseFloat(c), 0))





    However if you do not want to deal with the parsing in that function you can make sure that your array comes out as array of numbers like this:



    Array.from([$(".rightcell.emphasize").text().split('€')], (x) => parseFloat(x || 0))


    Which would get your array ready for summation and without the need to parse inside the Array.reduce. So it would be something like this:






    var strings = [,'102.80','192.60','22.16'];
    var numbers = Array.from(strings, (x) => parseFloat(x || 0))

    console.log(numbers.reduce((r,c) => r + c, 0))





    But in your case it would be shorter since you would do the first 2 lines as one as shown in the 2nd code snippet.






    share|improve this answer















    Since your array is composed of undefined and a bunch of strings you have to parse the values to get the numbers. The answer would be:






    var data = [,'102.80','192.60','22.16'];

    console.log(data.reduce((r,c) => r + parseFloat(c), 0))





    However if you do not want to deal with the parsing in that function you can make sure that your array comes out as array of numbers like this:



    Array.from([$(".rightcell.emphasize").text().split('€')], (x) => parseFloat(x || 0))


    Which would get your array ready for summation and without the need to parse inside the Array.reduce. So it would be something like this:






    var strings = [,'102.80','192.60','22.16'];
    var numbers = Array.from(strings, (x) => parseFloat(x || 0))

    console.log(numbers.reduce((r,c) => r + c, 0))





    But in your case it would be shorter since you would do the first 2 lines as one as shown in the 2nd code snippet.






    var data = [,'102.80','192.60','22.16'];

    console.log(data.reduce((r,c) => r + parseFloat(c), 0))





    var data = [,'102.80','192.60','22.16'];

    console.log(data.reduce((r,c) => r + parseFloat(c), 0))





    var strings = [,'102.80','192.60','22.16'];
    var numbers = Array.from(strings, (x) => parseFloat(x || 0))

    console.log(numbers.reduce((r,c) => r + c, 0))





    var strings = [,'102.80','192.60','22.16'];
    var numbers = Array.from(strings, (x) => parseFloat(x || 0))

    console.log(numbers.reduce((r,c) => r + c, 0))






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 27 '18 at 17:35

























    answered Nov 27 '18 at 17:29









    AkrionAkrion

    9,52011224




    9,52011224













    • you da man, well done sir

      – artworkjpm
      Nov 28 '18 at 9:08



















    • you da man, well done sir

      – artworkjpm
      Nov 28 '18 at 9:08

















    you da man, well done sir

    – artworkjpm
    Nov 28 '18 at 9:08





    you da man, well done sir

    – artworkjpm
    Nov 28 '18 at 9:08













    0














    Your elements in the array are treated as strings, therefore the + in your reduce function concatenates the array elements into another string. Try to use parseFloat() to treat them as numbers:



    var sum = array_new.reduce((a, b) => a + parseFloat(b), 0);


    EDIT: Thanks to charlietfl who mentioned, that parseFloat(a) is redundant, fixed it.



    EDIT: (since you already deleted your comment, here would be the solution for your problem): in your case this solution won't work, because one of your array elements is "" (an empty string) which can't be treated as a number, so you could map your array values before you try to add them up:



    array_new.map(a => (a == "") ? "0" : a);





    share|improve this answer


























    • parseFloat(a) is redundant since accumulator is number

      – charlietfl
      Nov 27 '18 at 15:50
















    0














    Your elements in the array are treated as strings, therefore the + in your reduce function concatenates the array elements into another string. Try to use parseFloat() to treat them as numbers:



    var sum = array_new.reduce((a, b) => a + parseFloat(b), 0);


    EDIT: Thanks to charlietfl who mentioned, that parseFloat(a) is redundant, fixed it.



    EDIT: (since you already deleted your comment, here would be the solution for your problem): in your case this solution won't work, because one of your array elements is "" (an empty string) which can't be treated as a number, so you could map your array values before you try to add them up:



    array_new.map(a => (a == "") ? "0" : a);





    share|improve this answer


























    • parseFloat(a) is redundant since accumulator is number

      – charlietfl
      Nov 27 '18 at 15:50














    0












    0








    0







    Your elements in the array are treated as strings, therefore the + in your reduce function concatenates the array elements into another string. Try to use parseFloat() to treat them as numbers:



    var sum = array_new.reduce((a, b) => a + parseFloat(b), 0);


    EDIT: Thanks to charlietfl who mentioned, that parseFloat(a) is redundant, fixed it.



    EDIT: (since you already deleted your comment, here would be the solution for your problem): in your case this solution won't work, because one of your array elements is "" (an empty string) which can't be treated as a number, so you could map your array values before you try to add them up:



    array_new.map(a => (a == "") ? "0" : a);





    share|improve this answer















    Your elements in the array are treated as strings, therefore the + in your reduce function concatenates the array elements into another string. Try to use parseFloat() to treat them as numbers:



    var sum = array_new.reduce((a, b) => a + parseFloat(b), 0);


    EDIT: Thanks to charlietfl who mentioned, that parseFloat(a) is redundant, fixed it.



    EDIT: (since you already deleted your comment, here would be the solution for your problem): in your case this solution won't work, because one of your array elements is "" (an empty string) which can't be treated as a number, so you could map your array values before you try to add them up:



    array_new.map(a => (a == "") ? "0" : a);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 27 '18 at 16:03

























    answered Nov 27 '18 at 15:46









    NyzeNyze

    13




    13













    • parseFloat(a) is redundant since accumulator is number

      – charlietfl
      Nov 27 '18 at 15:50



















    • parseFloat(a) is redundant since accumulator is number

      – charlietfl
      Nov 27 '18 at 15:50

















    parseFloat(a) is redundant since accumulator is number

    – charlietfl
    Nov 27 '18 at 15:50





    parseFloat(a) is redundant since accumulator is number

    – charlietfl
    Nov 27 '18 at 15:50











    0














    That's because your original array is a string array.



    Use parseFloat to create numbers from the strings.



    var strArr = ['102.80','192.60','22.16'];
    var sum = strArr.reduce((a,b) => a + parseFloat(b),0); // 317.56





    share|improve this answer


























    • Why create a new array with map()?

      – charlietfl
      Nov 27 '18 at 15:48











    • just for the example..if your into optimizations then you should use for instead of reduce.

      – Amir Popovich
      Nov 27 '18 at 15:52











    • Also the outer shouldn't be there

      – charlietfl
      Nov 27 '18 at 15:54
















    0














    That's because your original array is a string array.



    Use parseFloat to create numbers from the strings.



    var strArr = ['102.80','192.60','22.16'];
    var sum = strArr.reduce((a,b) => a + parseFloat(b),0); // 317.56





    share|improve this answer


























    • Why create a new array with map()?

      – charlietfl
      Nov 27 '18 at 15:48











    • just for the example..if your into optimizations then you should use for instead of reduce.

      – Amir Popovich
      Nov 27 '18 at 15:52











    • Also the outer shouldn't be there

      – charlietfl
      Nov 27 '18 at 15:54














    0












    0








    0







    That's because your original array is a string array.



    Use parseFloat to create numbers from the strings.



    var strArr = ['102.80','192.60','22.16'];
    var sum = strArr.reduce((a,b) => a + parseFloat(b),0); // 317.56





    share|improve this answer















    That's because your original array is a string array.



    Use parseFloat to create numbers from the strings.



    var strArr = ['102.80','192.60','22.16'];
    var sum = strArr.reduce((a,b) => a + parseFloat(b),0); // 317.56






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 27 '18 at 16:05

























    answered Nov 27 '18 at 15:45









    Amir PopovichAmir Popovich

    20.3k63868




    20.3k63868













    • Why create a new array with map()?

      – charlietfl
      Nov 27 '18 at 15:48











    • just for the example..if your into optimizations then you should use for instead of reduce.

      – Amir Popovich
      Nov 27 '18 at 15:52











    • Also the outer shouldn't be there

      – charlietfl
      Nov 27 '18 at 15:54



















    • Why create a new array with map()?

      – charlietfl
      Nov 27 '18 at 15:48











    • just for the example..if your into optimizations then you should use for instead of reduce.

      – Amir Popovich
      Nov 27 '18 at 15:52











    • Also the outer shouldn't be there

      – charlietfl
      Nov 27 '18 at 15:54

















    Why create a new array with map()?

    – charlietfl
    Nov 27 '18 at 15:48





    Why create a new array with map()?

    – charlietfl
    Nov 27 '18 at 15:48













    just for the example..if your into optimizations then you should use for instead of reduce.

    – Amir Popovich
    Nov 27 '18 at 15:52





    just for the example..if your into optimizations then you should use for instead of reduce.

    – Amir Popovich
    Nov 27 '18 at 15:52













    Also the outer shouldn't be there

    – charlietfl
    Nov 27 '18 at 15:54





    Also the outer shouldn't be there

    – charlietfl
    Nov 27 '18 at 15:54











    0














    in javascript, you can concat numbers by + or strings as well .
    I will give you an example :



    var x = "2" + "1"; // x = 21
    var x = 2 + 1 ; // x = 3


    You can look to this.



    You should convert your array to integers (parseInt) or floats(parseFloat)
    in this function array_new.reduce((a, b) => a + b, 0);
    It will be something like



    array_new.reduce((a, b) => a + parseInt(b), 0);//or
    array_new.reduce((a, b) => a + parseFloat(b), 0);





    share|improve this answer




























      0














      in javascript, you can concat numbers by + or strings as well .
      I will give you an example :



      var x = "2" + "1"; // x = 21
      var x = 2 + 1 ; // x = 3


      You can look to this.



      You should convert your array to integers (parseInt) or floats(parseFloat)
      in this function array_new.reduce((a, b) => a + b, 0);
      It will be something like



      array_new.reduce((a, b) => a + parseInt(b), 0);//or
      array_new.reduce((a, b) => a + parseFloat(b), 0);





      share|improve this answer


























        0












        0








        0







        in javascript, you can concat numbers by + or strings as well .
        I will give you an example :



        var x = "2" + "1"; // x = 21
        var x = 2 + 1 ; // x = 3


        You can look to this.



        You should convert your array to integers (parseInt) or floats(parseFloat)
        in this function array_new.reduce((a, b) => a + b, 0);
        It will be something like



        array_new.reduce((a, b) => a + parseInt(b), 0);//or
        array_new.reduce((a, b) => a + parseFloat(b), 0);





        share|improve this answer













        in javascript, you can concat numbers by + or strings as well .
        I will give you an example :



        var x = "2" + "1"; // x = 21
        var x = 2 + 1 ; // x = 3


        You can look to this.



        You should convert your array to integers (parseInt) or floats(parseFloat)
        in this function array_new.reduce((a, b) => a + b, 0);
        It will be something like



        array_new.reduce((a, b) => a + parseInt(b), 0);//or
        array_new.reduce((a, b) => a + parseFloat(b), 0);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 16:34









        Houssein ZouariHoussein Zouari

        383210




        383210






























            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%2f53503167%2fadd-up-numbers-in-array%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

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks