add up numbers in array
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
add a comment |
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
You are creating an array within an array by putting square brackets around your assignment ofarray_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
add a comment |
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
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
javascript arrays reduce
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 ofarray_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
add a comment |
You are creating an array within an array by putting square brackets around your assignment ofarray_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
add a comment |
4 Answers
4
active
oldest
votes
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.
you da man, well done sir
– artworkjpm
Nov 28 '18 at 9:08
add a comment |
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);
parseFloat(a)
is redundant since accumulator is number
– charlietfl
Nov 27 '18 at 15:50
add a comment |
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
Why create a new array withmap()
?
– 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 outershouldn't be there
– charlietfl
Nov 27 '18 at 15:54
add a comment |
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);
add a comment |
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
});
}
});
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%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
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.
you da man, well done sir
– artworkjpm
Nov 28 '18 at 9:08
add a comment |
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.
you da man, well done sir
– artworkjpm
Nov 28 '18 at 9:08
add a comment |
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.
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))
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
add a comment |
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
add a comment |
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);
parseFloat(a)
is redundant since accumulator is number
– charlietfl
Nov 27 '18 at 15:50
add a comment |
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);
parseFloat(a)
is redundant since accumulator is number
– charlietfl
Nov 27 '18 at 15:50
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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
Why create a new array withmap()
?
– 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 outershouldn't be there
– charlietfl
Nov 27 '18 at 15:54
add a comment |
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
Why create a new array withmap()
?
– 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 outershouldn't be there
– charlietfl
Nov 27 '18 at 15:54
add a comment |
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
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
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 withmap()
?
– 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 outershouldn't be there
– charlietfl
Nov 27 '18 at 15:54
add a comment |
Why create a new array withmap()
?
– 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 outershouldn'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
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 27 '18 at 16:34
Houssein ZouariHoussein Zouari
383210
383210
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.
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%2f53503167%2fadd-up-numbers-in-array%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
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