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

161
Visualizações
How to access datavalues using Map Function?

I have a "data" state which stores the values taken from my database.

const [data, setData] = useState([]);

"data" contains an array of objects which was extracted from my database.

// so data is essentially (if I console.log it): 
data = [
{student:"Jeff",country:"London"}, 
{student:"Benjamin",country:"France"},
{student:"Franklin",country:"USA"}
];

I am using React Select to display the labels of this data (I only want "student" data) but I am not sure how to map this data into values and labels to put them as options in my React Select component. I have tried the following but it gives me an error.

import Select from "react-select";
function myTable() {
   const myOptions = data.map(info => {
       value: {info.student},
       label: {info.student}
   });

   return (
     <tr>
        <td>
            <Select isMulti options={myOptions}></Select>
        </td>
     </tr>
   )
};

I want my options to contain values and labels from my data state object.

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

You don't need to add the info.student in an object.

import Select from "react-select";
function myTable() {
   const myOptions = data.map(info => ({
       value: info.student,
       label: info.student
   }));

   return (
     <tr>
        <td>
            <Select isMulti options={myOptions}></Select>
        </td>
     </tr>
   )

Codesandbox example

about 4 years ago · Santiago Gelvez Relatório

0

You have syntax error with your Array.prototype.map function. It should be:

const myOptions = data.map(info => ({
  value: info.student,
  label: info.student
}));

// or
const myOptions = data.map(info => {
  return {
    value: info.student,
    label: info.student
  }
});
about 4 years ago · Santiago Gelvez 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