Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
How to insert a name in an array with number values

I'm trying to render out the lowest value of three values in an array. It looks like this

  const compareValues = [
    companyResult.physicalAverage,
    companyResult.stressAverage,
    companyResult.developmentAverage,
  ];
  const getLowestValue = (Math.min(...compareValues)); // returns 4.5 (which is correct)

  return ( 
  <div>
    <p>{getLowestValue}</p> // HERE I want to render out the name (string) instead of 4.5 
  </div>
);

However, I want to name each property, i.e

companyResult.physicalAverage = "Physical Activity" 
companyResult.stressAverage = "Stress",
companyResult.developmentAverage = "Personal Development" 

I still want to return the lowest value with Math.min, but somehow return the 4.5 as a name. Anyone that has a clue on how?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Get the min value yourself.

const compareValues = [
    {name: 'physicalAverage', value: companyResult.physicalAverage},
    {name: 'stressAverage', value: companyResult.stressAverage},
    {name: 'developmentAverage', value: companyResult.developmentAverage},
];

let minObj = compareValues[0]

compareValues.forEach(obj => {
    if(obj.value < minObj.value)
        minObj = obj;
})

return ( 
  <div>
    <p>{minObj.name}</p> 
  </div>
);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use useMemo hooks to apply sort function. But you will have to tweak your compareValues array a bit to include details about title.

const {useMemo} = React;

const companyResult = {
  physicalAverage: 4.5,
  stressAverage: 5,
  developmentAverage: 6
};

function App() {
  const compareValues = useMemo(
    () => [
      { value: companyResult.physicalAverage, title: "Physical Activity" },
      { value: companyResult.stressAverage, title: "Stress" },
      { value: companyResult.developmentAverage, title: "Personal Development" }
    ],
    []
  );
  const lowest = useMemo(() => {
    return compareValues.sort((a, b) => a.value < b.value)[0];
  }, [compareValues]);

  return (
    <div>
      <p>Label: {lowest.title}</p>
      <p>Value: {lowest.value.toString()}</p>
    </div>
  );
}

ReactDOM.createRoot(
    document.getElementById("root")
).render(
    <App />
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.1.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.1.0/umd/react-dom.development.js"></script>
<div id="root"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to change your data structure. And then create a custom function (findMinValue) to find the minimum value in your data.

const values = [
  { name: "Physical Activity", value: companyResult.physicalAverage },
  { name: "Stress", value: companyResult.stressAverage },
  { name: "Personal Development", value: companyResult.developmentAverage },
];

const findMinValue = (array) =>
  array.reduce((prev, curr) => (prev.value < curr.value ? prev : curr));

const lowestValue = findMinValue(values);

return (
  <div>
    <p>{lowestValue.name}</p>
  </div>
);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda