1

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

1
  • I mentioned what i have tried which is const max = predictScore.reduce((acc, item) => acc = acc > item.prob ? acc : item.prob, 0); Commented Jul 22, 2021 at 5:28

1 Answer 1

1

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>`
);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.