简体   繁体   English

比较js中两个数组的键值

[英]Compare key values from two arrays in js

I have two arrays, one is answers and the other is correct . 我有两个数组,一个是answers ,另一个是correct Both are exactly the same, the only difference is that the answers fills from the user input, so what I want to do is compare this array with the array that already has the solutions so I can give back to the user a score. 两者都完全相同,唯一的区别是answers来自用户输入,所以我想要做的是将这个数组与已经有解决方案的数组进行比较,这样我就可以给用户一个分数。

I did this function to compare them: 我做了这个功能来比较它们:

 const correct = [{ slot1: "item1", slot2: "item2", slot3: "item3", slot4: "item4", slot5: "item5", slot6: "item6", slot7: "item7", slot8: "item8" }]; var answers = { slot1: "", slot2: "", slot3: "", slot4: "", slot5: "", slot6: "", slot7: "", slot8: "" }; function outputTest(){ //console.log(answers); compare(); } function compare(){ for (var[key, value] of Object.entries(correct)){ console.log(answers[key]); console.log(correct[key]); if (correct[key] == answers[key]){ score += 1; } } console.log("total score= "+score); } outputTest(); 

But it seems like it doesn't get "answers" array, it shows "undefined", even tho I tested it and it inserts the values correctly before. 但它似乎没有得到“答案”数组,它显示“未定义”,即使我测试它,它之前正确插入值。 Is this the right way to achieve this? 这是实现这一目标的正确方法吗?

  • Grab all the keys for the answers and perform reduce on them, with the initial total being zero. 获取答案的所有键并对其进行减少,初始总数为零。
  • If the key from the answers matching the key from the object in the correct array, increment the total 如果答案中的键与正确数组中的对象的键匹配,则递增总计

 const correct = [{ slot1: "item1", slot2: "item2", slot3: "item3", slot4: "item4", slot5: "item5", slot6: "item6", slot7: "item7", slot8: "item8" }]; var answers = { slot1: "", slot2: "item2", slot3: "", slot4: "", slot5: "", slot6: "item6", slot7: "item7", slot8: "" }; function outputTest() { compare(); } function compare() { var total = Object.keys(answers).reduce(function(total, answerKey) { if (answers[answerKey] === correct[0][answerKey]) { total++; } return total; }, 0); console.log("total score = " + total); } outputTest(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JS - 按键比较 2 arrays,返回 4 arrays:匹配和不匹配 - JS - compare 2 arrays by key, return 4 arrays: matches & unmatches from each 如何比较两个对象数组并更正相同的键值? - How to compare two object arrays and correct identical key values? 比较两个对象数组JS - Compare two arrays of objects JS 比较两个数组,并用第三个数组中的值替换重复项 - Compare two Arrays and replace Duplicates with values from a third array 如何比较 JavaScript 中的两个 arrays? 但关键是一个字符串 - How to compare two arrays in JavaScript? But the key is a string JS - 嵌套 for 循环 - 按键比较 2 个 arrays,匹配和不匹配 - JS - nested for loops - compare 2 arrays by key, return 4 new arrays, matches & unmatches from each 在2个数组中仅比较键值一次 - Compare key values only once in 2 arrays 如何在匹配键值的基础上比较两个不同长度和键的数组? - How can i compare two arrays of different length and keys on the basis of matching key values? JavaScript比较两个数组(键/值对),如果键匹配,则将值从一个复制到另一个 - JavaScript compare two arrays(key/value pairs) and copy value from one to the other if key matches 将对象与来自两个不同对象数组的相应键值合并 - Merge objects with corresponding key values from two different arrays of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM