Sum of array, fetched data from database
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
add a comment |
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
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 movealert
outside the loop
– Nitish Narang
Nov 25 '18 at 13:09
add a comment |
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
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
javascript arrays
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 movealert
outside the loop
– Nitish Narang
Nov 25 '18 at 13:09
add a comment |
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 movealert
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
add a comment |
4 Answers
4
active
oldest
votes
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.
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 changeel.itemPrice
toNumber(el.itemPrice)
– Nitish Narang
Nov 25 '18 at 13:16
It works now thank you!
– bobby
Nov 25 '18 at 13:18
add a comment |
Use the following :
for (var i = 0; i < arr.length; i++) {
sum += arr[i].itemPrice;
}
alert(sum);
Alert the sum after the loop completes.
add a comment |
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))
add a comment |
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);
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%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
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.
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 changeel.itemPrice
toNumber(el.itemPrice)
– Nitish Narang
Nov 25 '18 at 13:16
It works now thank you!
– bobby
Nov 25 '18 at 13:18
add a comment |
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.
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 changeel.itemPrice
toNumber(el.itemPrice)
– Nitish Narang
Nov 25 '18 at 13:16
It works now thank you!
– bobby
Nov 25 '18 at 13:18
add a comment |
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.
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.
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 changeel.itemPrice
toNumber(el.itemPrice)
– Nitish Narang
Nov 25 '18 at 13:16
It works now thank you!
– bobby
Nov 25 '18 at 13:18
add a comment |
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 changeel.itemPrice
toNumber(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
add a comment |
Use the following :
for (var i = 0; i < arr.length; i++) {
sum += arr[i].itemPrice;
}
alert(sum);
Alert the sum after the loop completes.
add a comment |
Use the following :
for (var i = 0; i < arr.length; i++) {
sum += arr[i].itemPrice;
}
alert(sum);
Alert the sum after the loop completes.
add a comment |
Use the following :
for (var i = 0; i < arr.length; i++) {
sum += arr[i].itemPrice;
}
alert(sum);
Alert the sum after the loop completes.
Use the following :
for (var i = 0; i < arr.length; i++) {
sum += arr[i].itemPrice;
}
alert(sum);
Alert the sum after the loop completes.
answered Nov 25 '18 at 13:03
Nicholas KNicholas K
7,76661537
7,76661537
add a comment |
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
answered Nov 25 '18 at 13:08
Nitish NarangNitish Narang
2,9601815
2,9601815
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 25 '18 at 13:10
kemicofakemicofa
10.7k43984
10.7k43984
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%2f53467706%2fsum-of-array-fetched-data-from-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
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