繁体   English   中英

比较两个对象 typescript 并检查是否有三个或更多值相同

[英]Comparing two objects typescript and check if there three or more value same

大家好,有人可以帮助我,我什至不知道从哪里开始,很少的帮助很好。 我有这样的 object

const Questions = {
 five: "c"
 four: "c"
 one: "a"
 three: "a"
 two: "a"
};

我有一些像这样的 object

const Answers = {
 five: "a"
 four: "a"
 one: "a"
 three: "a"
 two: "a"
};

我需要比较这两个对象,看看是否有 3 个或更多匹配项正确,并在此基础上显示不同的消息。 我知道我可以像这样比较两个对象

JSON.stringify(Questions) === JSON.stringify(Answers);

它会返回我只是对还是错,我也尝试返回刚刚正确的答案,但我再次没有运气,像这样

  checkCorrectAnswers(obj1: any, obj2: any): any {
    const keys1 = [];
    const values1 = [];
    Object.keys(obj1).forEach((element) => {
      keys1.push(element);
    });
    Object.values(obj1).forEach((element) => {
      values1.push(element);
    });
    const keys2 = [];
    const values2 = [];
    Object.keys(obj2).forEach((element) => {
      keys2.push(element);
    });
    Object.values(obj2).forEach((element) => {
      values2.push(element);
    });
    const obj = {};
    keys1.forEach((element, i) => {
      for (let index = 0; index < keys2.length; index++) {
        if (element === keys2[index]) {
          if (values1[i] !== values2[index]) {
            const jsonKey = element;
            obj[jsonKey] = values1[i];
          }
          break;
        }
      }
    });
    return obj;
  }

再次感谢答案。

您可以遍历问题并检查答案,如下所示:

const Answers = {
 five: "a",
 four: "a",
 one: "a",
 three: "a",
 two: "a"
};

const Questions = {
 five: "c",
 four: "c",
 one: "a",
 three: "a",
 two: "a"
};

const isPass = (passingScore) => {
  let score = 0

  for (const questionKey in Questions) {
    const questionValue = Questions[questionKey];
    const answerValue = Answers[questionKey]

    const isCorrect = answerValue === questionValue

    if (!answerValue) {
      continue
    }

    if (isCorrect) {
      score++
    }
  }

  return score >= passingScore
}

这样做的好处是它只需要一个循环。

您可以通过这种方式计算正确答案。

 const Questions = {five:"c",four:"c",one:"a",three:"a",two:"a"}; const Answers = {five:"a",four:"a",one:"a",three:"a",two:"a"}; const props = Object.keys(Questions); const number_Of_Correct_Answers = props.reduce((acc, curr_prop) => { if(Questions[curr_prop] === Answers[curr_prop]) acc++; return acc; }, 0); console.log(number_Of_Correct_Answers);

const Questions = {five: "c", four: "c", one: "a", three: "a", two: "a"};
const Answers = {five: "a", four: "a", one: "a", three: "a", two: "a"};
rightAnswers = Object.fromEntries(Object.entries(Answers).filter(([k, v]) => Questions[k] === v));
wrongAnswers = Object.fromEntries(Object.entries(Answers).filter(([k, v]) => Questions[k] !== v));
console.log("Right answers:", rightAnswers);
console.log("Wrong answers:", wrongAnswers);
console.log("Passed:", Object.keys(rightAnswers).length >= 3);

Output

Right answers: { one: 'a', three: 'a', two: 'a' }
Wrong answers: { five: 'a', four: 'a' }
Passed: true

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM