Here is a code snippet that checks whether two arrays of objects are equal or not. How can we enhance this code to log which answer is not matching?
The structure of the arrays: arrayA represents user answered questions, while arrayB contains correct answers.
arrayA: { id: number; answer: number }[] = [];
arrayB: { id: number; answer: number }[] = [];
Function for comparing arrays:
arraysEqual(arrayA, arrayB)
arraysEqual = (a1, a2) =>
a1.length === a2.length &&
a1.every((o, idx) => this.objectsEqual(o, a2[idx]));
objectsEqual = (o1, o2) =>
typeof o1 === "object" && Object.keys(o1).length > 0
? Object.keys(o1).length === Object.keys(o2).length &&
Object.keys(o1).every(p => this.objectsEqual(o1[p], o2[p]))
: o1 === o2;