admin管理员组文章数量:1287579
At first I have have made a normal function to get the sum of the array elements [1,2,3,4,5,6,7,8,9] and it works I got 45 as output.. After that I have tried the same logic with map() method, so that I can put the sum result in an array. but in the map() method instead of giving me the sum of the all array elements its gave me this output [1,3,5,7,9,11,13].If I made any mistake to build the logic, could you please explain me to get the addition of array elements what would be the logic if I want to use map() method. below I attached the code.
//normal function to get the sum of array numbers
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
function add(nums) {
let sum = 0;
for (const num of nums) {
sum = sum + num;
}
return sum;
}
console.log(add(array));
//map() method
const numbers = [1, 2, 3, 4, 5, 6, 7];
let add = numbers.map((num, sum = 0) => sum = sum + num);
console.log(add);
At first I have have made a normal function to get the sum of the array elements [1,2,3,4,5,6,7,8,9] and it works I got 45 as output.. After that I have tried the same logic with map() method, so that I can put the sum result in an array. but in the map() method instead of giving me the sum of the all array elements its gave me this output [1,3,5,7,9,11,13].If I made any mistake to build the logic, could you please explain me to get the addition of array elements what would be the logic if I want to use map() method. below I attached the code.
//normal function to get the sum of array numbers
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
function add(nums) {
let sum = 0;
for (const num of nums) {
sum = sum + num;
}
return sum;
}
console.log(add(array));
//map() method
const numbers = [1, 2, 3, 4, 5, 6, 7];
let add = numbers.map((num, sum = 0) => sum = sum + num);
console.log(add);
Share
Improve this question
edited Aug 30, 2022 at 12:35
Nafiz Imtiaz
asked Aug 30, 2022 at 12:29
Nafiz ImtiazNafiz Imtiaz
531 gold badge1 silver badge5 bronze badges
2
-
4
"Trying to get all numbers sum form an array by map() method"
map
is just fundamentally the wrong method for this. Use a loop (directly, or hidden away in a call toreduce
), butmap
makes no more sense thanfilter
for an operation that doesn't create a new array with mapped elements. – T.J. Crowder Commented Aug 30, 2022 at 12:32 - .reduce() method easier way to sum elements inside an array and you can do it in a single line of code – Chris G Commented Aug 30, 2022 at 12:37
1 Answer
Reset to default 9You can't sum with map
. map
always returns a new array with the same length.
You wanted to use reduce
const numbers = [1, 2, 3, 4, 5, 6, 7];
const result = numbers.reduce((sum, num) => sum + num);
console.log(result);
As T.J. Crowder suggested in the ment, skipping the second argument can lead to a failure if the array is empty
const numbers = [];
const result = numbers.reduce((sum, num) => sum + num);
console.log(result);
So it's better to use it if you can
const numbers = [];
const result = numbers.reduce((sum, num) => sum + num, 0);
console.log(result);
本文标签: javascriptTrying to get all numbers sum form an array by map() methodStack Overflow
版权声明:本文标题:javascript - Trying to get all numbers sum form an array by map() method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741238308a2363463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论