I have an array of objects. In each object there is a details property (array) with more than one object - for this example I am only showing one.
I am looking for the rating property in each details and looking to find minimum value (in this example 3.1) ... is there a simpler/cleaner way of achieving this?
const ratings = [{ id: 'ABC', details: [{ type: 'VALUE', rating: 9.5 }] }, { id: 'DEF', details: [{ type: 'VALUE', rating: 3.1 }] }, { id: 'GHI', details: [{ type: 'VALUE', rating: 4.5 }] }]
const ids = ['ABC', 'DEF', 'GHI']
const array = []
ids.forEach(element => {
const valueScore = ratings?.find(r => r.id === element)?.details?.find(c => c.type === 'VALUE')
if (valueScore.rating) {
array.push(valueScore.rating)
}
})
console.log('MIN VALUE', Math.min(...array))
findwhy not just iterate once throughratings, also if your code works and you want feedback/suggestions you might want to look at the code review SE site