Emanuel Quimper Emanuel Quimper - 2 months ago 5
Javascript Question

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

I need to iterate over two arrays and see if the arr1 with index 1 is the same value of the arr 2 index 1 etc...

My first array is an array of an object who contains a solution.

My second array is only answer and this is a just array of string.

I need solution match answer.

I was thinking about

.filter()
but can I keep the index ?

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;
}

Answer

Why not use a for loop and access the two arrays by index?

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