Reputation: 754
i have the below array 'predictScore' and able to get the max value using const max = predictScore.reduce((acc, item) => acc = acc > item.prob ? acc : item.prob, 0);
How do i get the max value and associated label and render in a p tag as:
<p>The image uploaded is {max} percentage of {label} </p>
predictScore = [
0: {label: "other", prob: 0.03129873052239418}
1: {label: "dog", prob: 0.033306580036878586}
2: {label: "cat", prob: 0.9353946447372437}
]
So based on the above cat score is 93% so the final output should be The image uploaded is 93% percentage of a cat
Upvotes: 1
Views: 67
Reputation: 202846
You are close, just return the entire item
object and check the prob
property in the reduce callback.
const predictScore = [
{ label: "other", prob: 0.03129873052239418 },
{ label: "dog", prob: 0.033306580036878586 },
{ label: "cat", prob: 0.9353946447372437 }
];
const { label, prob } = predictScore.reduce(
(acc, item) => (acc.prob > item.prob ? acc : item),
{}
);
console.log(
`<p>The image uploaded is ${Math.round(
prob * 100
)}% percentage of ${label} </p>`
);
Upvotes: 1