Find object by key in array of object
The value you return in the reduce()
callback is used as the accumulator in the next iteration. Since you don't return anything when the key doesn't match raceId
, your code will only work if the matching key is the last element of the array.
Use find()
rather than reduce()
, and then get the property from that.
const found = raceScoringConfigGroups.find(el => el[raceId]);
const reduced = found?.[raceId];