Sum of two arrays

Joined
Jul 9, 2023
Messages
25
Reaction score
0
Hello

I'm trying to find the sum of two arrays.
but, NaN error occurs.
I think it's because the elements of the array are mixed with characters.

How do I solve this problem?

DELETE1.png
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
If you're trying to add the 202309:10 itself you need to do this element by element,

BTW Please use the </> icon above and insert code so we can copy it easily. Pasting a picture doesn't help much.
 
Joined
Jul 4, 2023
Messages
474
Reaction score
58
I'm trying to find the sum of two arrays.
what you mean about sum two arrays, you want merge (concatenate), because in javascript + operator you can only use to add numbers or concatenate strings.

e.g.
JavaScript:
const array1 = [
  { Name:'James', Subject:'English', 202309:10, 202310:30, 202311:50 },
  { Name:'Neoh',  Subject:'Math'   , 202309:20, 202310:15, 202311:90 }
];

const array2 = [
  { Name:'James', Subject:'English', 202309:10, 202310:30, 202311:50 },
  { Name:'Neoh',  Subject:'Math'   , 202309:20, 202310:15, 202311:90 }
];

const combinedArray1 = array1.concat(array2); //
console.log(combinedArray1);

const combinedArray2 = [...array1, ...array2]; // using the spread operator
console.log(combinedArray2);

or if you want just adding values of 202309:10, 202310:30, 202311:50 for every singel person try use for example a map.

e.g.
JavaScript:
const array1 = [
  { Name:'James', Subject:'English', 202309:10, 202310:30, 202311:50 },
  { Name:'Neoh',  Subject:'Math'   , 202309:20, 202310:15, 202311:90 }
];

const array2 = [
  { Name:'James', Subject:'English', 202309:10, 202310:30, 202311:50 },
  { Name:'Neoh',  Subject:'Math'   , 202309:20, 202310:15, 202311:90 }
];

const arraySum = array1.map((item1, index) => {
  const item2 = array2[index];
  return {
    Name: item1.Name,
    Subject: item1.Subject,
    202309: item1[202309] + item2[202309],
    202310: item1[202310] + item2[202310],
    202311: item1[202311] + item2[202311],
  };
});

console.log(arraySum);

like said
to add the 202309:10 itself you need to do this element by element,
 
Last edited:
Joined
Jul 4, 2023
Messages
474
Reaction score
58
Here is a more universal algorithm,
[ on-line ]
JavaScript:
// Define two arrays with data
const array1 = [
  { Name: 'James', Subject: 'English', 202309: 10, 202310: 30, 202311: 50 },
  { Name: 'Neoh',  Subject: 'Math',    202309: 20, 202310: 15, 202311: 90 },
  { Name: 'Mateo', Subject: 'Chinese', 202310: 20, 202311: 5 }
];

const array2 = [
  { Name: 'James', Subject: 'English', 202309: 10, 202310: 30, 202311: 50 },
  { Name: 'Neoh',  Subject: 'Math',    202309: 20, 202310: 15, 202311: 90 },
  { Name: 'Mateo', Subject: 'Chinese', 202309: 80, 202311: 10 }
];

// Combine the two arrays into one
const combinedArray2 = [...array1, ...array2];
//console.log(combinedArray2);


// Initialize an empty array to store the summed data
const summedArray = combinedArray2.reduce((accumulator, current) => {
  // Check if an existing item with the same Name and Subject exists in the accumulator
  const existingItem = accumulator.find(item => item.Name === current.Name && item.Subject === current.Subject);

  if (existingItem) {
    // If an existing item is found, add the values of fields starting with '2023...'
    for (const key in current) {
      if (!existingItem[key]) existingItem[key] = 0;
      if (key.startsWith('2023')) existingItem[key] += current[key];
    }
  } else {
    // If no existing item is found, add a new entry to the result array
    accumulator.push(current);
  }
  return accumulator;
}, []);

console.log(summedArray);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top