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 to reduce), but map makes no more sense than filter 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
Add a ment  | 

1 Answer 1

Reset to default 9

You 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