I am faced with a task involving an array of objects. Each object has a key that needs to be used to search through sets of arrays containing similar objects. The goal is to determine if all the arrays contain the same value for a specific key in my object. If they do, I want to set that value as the object's value.
Is there a more efficient way to achieve this? Perhaps utilizing a powerful, elegant lodash function?
myValues[] = [{'id': 7, 'score': null}, {'id': 3, 'score': null}];
const searchme[] = /* data fetch*/;
// sample searchme[] =[
// [{'id': 1, 'score': 1}, {'id': 2, 'score': 1}, {'id': 3, 'score': 1}],
// [{'id': 1, 'score': 2}, {'id': 2, 'score': 1}, {'id': 4, 'score': 3}],
// [{'id': 1, 'score': 1}, {'id': 2, 'score': 1}, {'id': 3, 'score': 1}],
// ];
// searchme[] contains 1-5 arrays like myValues[];
// so searchme[0] = [{'id': 2, 'score': 2}, {'id': 7, 'score': 2}], and so on
// theoretically, each searchme[] array has all the ids all the others do
// - but a few might be missing
myValues.forEach(v => {
let score: number = null;
let started: boolean = false;
let fail: boolean = false;
searchme.forEach( anArray => {
found = anArray.find( search => search.id = v.id);
if (found) {
if (!started) { started = true; score = found.score; }
else { if (score != found.score) { fail = true; }
} else {
fail = true;
}
});
if (!fail) { v.score = score; }
});
The current solution works, but it feels quite inefficient. Any suggestions or lodash functions to enhance its performance?:)