我有2个数组,用户和问题,每个数组都有一个nip_nim(ID)。 Questions数组有数千行,每行分别用nip_nim和role_id区分。当两个数组(用户和问题)的nip_nim相同时,如何执行总和?
我试图将总和推到一个新的数组中,但是它总是被新的数组代替,并且总和值是整个数组的总和,而不是由单个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
});
});
});
}
电流输出: 在每次迭代中,它添加了一个新数组,而不是“仅”最终总值,并且重复的次数与具有相同nip_nim的用户数量相同(在我的当前数据库中,它重复了121次用户中的3次) / p>
newArray(0)
newArray(1)
...
newArray(468)
所需的输出
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.
];
答案 0 :(得分:0)
我在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce处找到了一种解决方法,方法是使用分组方式。
var people = [
{ name: 'Alice', age: 21 },
{ name: 'Max', age: 20 },
{ name: 'Jane', age: 20 }
];
function groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
var key = obj[property];
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(obj);
return acc;
}, {});
}
var groupedPeople = groupBy(people, 'age');
// groupedPeople is:
// {
// 20: [
// { name: 'Max', age: 20 },
// { name: 'Jane', age: 20 }
// ],
// 21: [{ name: 'Alice', age: 21 }]
// }
使用这种方法,我可以在每个自己的“数组”中执行求和,而不必费心处理不同的表,然后将最终结果推送到新数组中。