我想比较一本包含教练员特定偏好的字典和其他包含教练员个人资料的字典。
所以每个教练都得到一个分数,根据他与教练偏好的匹配程度,或者因为他不符合一个重要的偏好而被排除在外,得分为-1。
一个教练将与24个教练进行比较,并得到一个分数,所以我可以看到哪个是最好的匹配。
问题:我如何循环这个?
下面是一个代码示例://coachee\
var coachee = {
"geschlecht": 1,
"alter_option_1": 1,
"alter_option_2": 1,
"alter_option_3": 0,
"themen_option_1": 1,
"themen_option_2": 0,
"themen_option_3": 0,
"branche_option_1": 1,
"branche_option_2": 0,
"branche_option_3": 0,
"erfahrung_option_1": 1,
"erfahrung_option_2": 0,
"erfahrung_option_3": 1,
"sprache_option_1": 1,
"sprache_option_2": 0,
"sprache_option_3": 0,
}
//coach1\
var coach1 = {
coach1_answers_geschlecht : 2,
coach1_answers_alter_option_1 : 0,
coach1_answers_alter_option_2 : 1,
coach1_answers_alter_option_3 : 0,
coach1_answers_themen_option_1 : 1,
coach1_answers_themen_option_2 : 0,
coach1_answers_themen_option_3 : 1,
coach1_answers_branche_option1 : 1,
coach1_answers_branche_option2 : 1,
coach1_answers_branche_option3 : 0,
coach1_answers_erfahrung_option1 : 1,
coach1_answers_erfahrung_option2 : 1,
coach1_answers_erfahrung_option3 : 0,
coach1_answers_sprache_option_1 : 1,
coach1_answers_sprache_option_2 : 1,
coach1_answers_sprache_option_3 : 1,
};
///COMPARISON\
//Themen\
if (answers["themen_option_1"] == 1 && coach1_answers_themen_option_1 == 1) {
coach1_themen1_score = 1;
} else {
coach1_themen1_score = 0
}
if (answers["themen_option_2"] == 1 && coach1_answers_themen_option_2 == 1) {
coach1_themen2_score = 1;
} else {
coach1_themen2_score = 0
}
if (answers["themen_option_3"] == 1 && coach1_answers_themen_option_3 == 1) {
coach1_themen3_score = 1;
} else {
coach1_themen3_score = 0
}
var coach1_themen_score = coach1_themen1_score + coach1_themen2_score + coach1_themen3_score
//Branche\
if (answers["branche_option_1"] == 1 && coach1_answers_branche_option1 == 1) {
coach1_branche1_score = 1;
} else {
coach1_branche1_score = 0
}
if (answers["branche_option_2"] == 1 && coach1_answers_branche_option2 == 1) {
coach1_branche2_score = 1;
} else {
coach1_branche2_score = 0
}
if (answers["branche_option_3"] == 1 && coach1_answers_branche_option3 == 1) {
coach1_branche3_score = 1;
} else {
coach1_branche3_score = 0
}
var coach1_branche_score = coach1_branche1_score + coach1_branche2_score + coach1_branche3_score
//Erfahrung\
if (answers["erfahrung_option_1"] == 1 && coach1_answers_erfahrung_option1 == 1) {
coach1_erfahrung1_score = 1;
} else {
coach1_erfahrung1_score = 0
}
if (answers["erfahrung_option_2"] == 1 && coach1_answers_erfahrung_option2 == 1) {
coach1_erfahrung2_score = 1;
} else {
coach1_erfahrung2_score = 0
}if (answers["erfahrung_option_3"] == 1 && coach1_answers_erfahrung_option3 == 1) {
coach1_erfahrung3_score = 1;
} else {
coach1_erfahrung3_score = 0
}
var coach1_erfahrung_score = coach1_erfahrung1_score + coach1_erfahrung2_score + coach1_erfahrung3_score
//Score\
var coach1_score = (coach1_alter_score + coach1_themen_score + coach1_branche_score + coach1_erfahrung_score)*10
///EXCLUSION\
//Alter\
if (answers["alter_option_1"] == 1 && coach1_answers_alter_option_1 == 1) {
coach1_alter_score = 1;
} else if (answers["alter_option_2"] == 1 && coach1_answers_alter_option_2 == 1) {
coach1_alter_score = 1;
} else if (answers["alter_option_3"] == 1 && coach1_answers_alter_option_3 == 1) {
coach1_alter_score = 1;
} else {
coach1_score = -1
}
//Sprache\
if (answers["sprache_option_1"] == 1 && coach1_answers_sprache_option_1 == 1 ) {
coach1_sprache_score = 1;
} else if (answers["sprache_option_2"] == 1 && coach1_answers_sprache_option_2 == 1 ) {
coach1_sprache_score = 1;
} else if (answers["sprache_option_3"] == 1 ) {
coach1_sprache_score = 1;
} else {
coach1_score = -1;
}
//Geschlecht\
if (answers["geschlecht"] == 3) {
coach1_geschlecht_score = 1;
} else if (answers["geschlecht"] == coach1_answers_geschlecht) {
coach1_geschlecht_score = 1;
} else {
coach1_score = -1;
}
//Coach2\
var coach2 = {
coach2_answers_geschlecht : 2,
coach2_answers_alter_option_1 : 0,
coach2_answers_alter_option_2 : 1,
coach2_answers_alter_option_3 : 0,
coach2_answers_themen_option_1 : 1,
coach2_answers_themen_option_2 : 1,
coach2_answers_themen_option_3 : 1,
coach2_answers_branche_option1 : 1,
coach2_answers_branche_option2 : 1,
coach2_answers_branche_option3 : 0,
coach2_answers_erfahrung_option1 : 1,
coach2_answers_erfahrung_option2 : 1,
coach2_answers_erfahrung_option3 : 0,
coach2_answers_sprache_option_1 : 1,
coach2_answers_sprache_option_2 : 1,
coach2_answers_sprache_option_3 : 1,
};
作为结果,我想要一个数组的所有分数,如:
coaches_score = [coach1_score, coach2_score, ...]
手动我将重复//COMPARISON和//EXCLUSION块为每个教练,但我必须这样做24次。有什么方法可以循环这个吗?
谢谢大家。
我建议将数据结构更改为数组的可迭代风格,并使用相同的键来计算一组键的分数。
const
getScore = coach => {
const
score = {},
keys1 = ['themen', 'branche', 'erfahrung'],
keys2 = ['alter', 'sprache'];
keys1.forEach(k => score[k] = coachee[k].reduce((s, v, i) => s + v * coach[k][i], 0));
score.score = keys1.reduce((s, k) => s + score[k], 0) * 10;
keys2.forEach(k => {
score[k] = +coachee[k].slice(0, -1).some((v, i) => v * coach[k][i]) || +coach1[k][coach[k].length - 1] || 0;
if (!score[k]) score.score = -1;
});
score.geschlecht = +(coach.geschlecht === 3 || coachee.geschlecht === coach.geschlecht)
if (!score.geschlecht) score.score = -1;
return score;
},
coachee = {
geschlecht: 1,
alter: [1, 1, 0, 0],
themen: [1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
branche: [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0],
erfahrung: [1, 0, 1, 0, 0],
sprache: [1, 0, 0]
},
coach1 = {
geschlecht: 2,
alter: [0, 1, 0, 1],
themen: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1],
branche: [1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1],
erfahrung: [1, 1, 0, 0, 1],
sprache: [1, 1, 1]
};
console.log(getScore(coach1));
好吧,我仍然没有得到的代码,因为它是复杂的方式为我的Java脚本的理解,但我仍然得到它为我工作。我把这个编辑成代码:
var coach1_values = getScore(coach1)
var coach2_values = getScore(coach2)
var coach3_values = getScore(coach3);
//Summieren der Werte zum Score\
const sumValues = obj => Object.values(obj).reduce((a, b) => a + b);
if (coach1_values.score != -1) {
var coach1_score = sumValues(coach1_values);
} else coach1_score = -1;
if (coach2_values.score != -1) {
var coach2_score = sumValues(coach2_values);
} else coach2_score = -1;
if (coach3_values.score != -1) {
var coach3_score = sumValues(coach3_values);
} else coach3_score = -1;
我不明白为什么分数的值是110,如果它匹配"geschlecht",但这没有问题,因为每个人的分数都是110。再次谢谢你