迭代2个数组并找到匹配,但需要相同的索引

Iterate over 2 arrays and find match but need to be same index

本文关键字:索引 数组 2个 迭代      更新时间:2023-09-26

我需要遍历两个数组,看看索引为1的arr1是否与索引为1的arr1相同…

我的第一个数组是一个包含解的对象数组。

我的第二个数组只有答案,这是一个字符串数组。

我需要匹配答案的解决方案

我正在考虑.filter(),但是我可以保留索引吗?

const checkGoodAnswers = (qArr, rArr) => {
  const goodAnswers = qArr.filter(q => q.solutionToQuestion === rArr.map(x => x));
  const totalPoints = goodAnswers.reduce((sum, q) => sum + q.questionPoints, 0);
  return totalPoints;
}

为什么不使用for循环并通过索引访问两个数组?

for (i = 0; i < qArr; i++) { 
    // check if qArr[i] matches rArr[i]
}