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

164
Visualizações
How can I change the key value in an object when creating an array value while using the map function in React?

I have the data of the array object in a variable called hi[0].child.

hi[0].child = [
    {code: "food", name: "buger"},
    {code: "cloth", name: "outher"},
    {code: "fruit", name: "apple"},
]

What I want to do is create a variable called hello, change the key value of hi[0].child, and put it in the hello variable.

For example, the key value of code is changed to value and name is changed to label. The values ​​I want are as below.

expecter answer 

hello = [
    {label: "burger", value: "food"},
    {label: "outher", value: "cloth"},
    {label: "apple", value: "fruit"},

]

But when I use my code I get these errors.

Did not expect a type annotation here

so How can i fix my code?

this is my code

let hello = []


hi[0].child.map((v) => {
hello.push(label: v.name, value: v.code)
})
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You've missed the curly brackets for the object, it should be hello.push({ label: v.name, value: v.code }).

Also, map is not the right method to be used here, use forEach instead or use the value returned from map (as shown below).

const data = [
  { code: "food", name: "buger" },
  { code: "cloth", name: "outher" },
  { code: "fruit", name: "apple" },
];

const updatedData = data.map((v) => ({ label: v.code, value: v.name }));

console.log(updatedData);

about 4 years ago · Santiago Trujillo Relatório

0

Change the code like this

let hello = []
hi[0].child.map((v) => {
  hello.push({label: v.name, value: v.code})
})

You need to pass object

about 4 years ago · Santiago Trujillo 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