I have 2 arrays, users and questions, each has a nip_nim (ID). Questions array has thousands of rows each differentiate with nip_nim and role_id. How do I perform a total summation when the nip_nim in both array (users and questions) are the same?
I tried to push the total summation to a new array, but it's always replaced with the new one, and the summation value is the whole summation of array, not by individual nip_nim
export function hitungQuestions(questions, users) {
users.forEach(function(uItem) {
let P1 = 0;
let P2 = 0;
let P3 = 0;
let P4 = 0;
let data = [];
let i = 0;
questions.forEach(function(qItem) {
if (qItem.nip_nim === uItem.nip_nim) {
const role_id = qItem.role_id;
// each question from 1-30
const K001 = role_id === 1 ? qItem.K001 : 0;
const K002 = role_id === 1 ? qItem.K002 : 0;
const K003 = role_id === 1 ? qItem.K003 : 0;
const K004 = role_id === 1 ? qItem.K004 : 0;
const K005 = qItem.K005;
const K006 = qItem.K006;
const K007 = qItem.K007;
const K008 = qItem.K008;
const K009 = qItem.K009;
const K010 = qItem.K010;
const K011 = qItem.K011;
const K012 = qItem.K012;
const K013 = qItem.K013;
const K014 = qItem.K014;
const K015 = qItem.K015;
const K016 = qItem.K016;
const K017 = qItem.K017;
const K018 = qItem.K018;
const K019 = qItem.K019;
const K020 = qItem.K020;
const K021 = qItem.K021;
const K022 = qItem.K022;
const K023 = qItem.K023;
const K024 = qItem.K024;
const K025 = qItem.K025;
const K026 = qItem.K026;
const K027 = qItem.K027;
const K028 = qItem.K028;
const K029 = qItem.K029;
const K030 = qItem.K030;
// each question have different 'weight' based on role_id
// except for C1
const C1 = HitungC1(K001, K002, K003, K004);
const C2 = HitungC2(role_id, K005, K006, K007);
const C3 = HitungC3(role_id, K008, K009, K010);
const C4 = HitungC4(role_id, K011, K012, K013);
const C5 = HitungC5(role_id, K014, K015, K016, K017);
const C6 = HitungC6(role_id, K018, K019, K020, K021, K022, K023);
const C7 = HitungC7(role_id, K024, K025, K026, K027, K028, K029, K030);
const TotalNilai = C1 / 4 + C2 / 3 + C3 / 3 + C4 / 3 + C5 / 4 + C6 / 6 + C7 / 7;
if (role_id === 1) {
P1 = P1 + TotalNilai;
} else if (role_id === 2) {
P2 = P2 + TotalNilai;
} else if (role_id === 3) {
P3 = P3 + TotalNilai;
} else if (role_id === 4) {
P4 = P4 + TotalNilai;
}
// if value > 20, return 'Very Good', and etc...
const hasil = hitungHasil(TotalNilai);
}
data.push({
nip_nim: qItem.nip_nim,
P1: P1,
P2: P2,
P3: P3,
P4: P4
});
});
});
}
Current output: With each iteration, it added a new array instead 'only' the final total value, and this is repeat as much as the number of user who has the same nip_nim (in my current database, it repeats 3 times out of 121 user)
newArray(0)
newArray(1)
...
newArray(468)
Desired output
newArray = [
{
nip_nim: nip_nim_1,
P1: P1, // a total of P1 from nip_nim_1
P2: P2, // a total of P2 from nip_nim_1
P3: P3, // a total of P3 from nip_nim_1
P4: P4, // a total of P4 from nip_nim_1
Total: Total // a total of P from nip_nim_1
},
{
nip_nim: nip_nim_2,
P1: P1, // a total of P1 from nip_nim_2
P2: P2, // a total of P2 from nip_nim_2
P3: P3, // a total of P3 from nip_nim_2
P4: P4, // a total of P4 from nip_nim_2
Total: Total // a total of P from nip_nim_1
}, etc.
];