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

110
Visualizações
How to map through array of objects and create a new array of objects with a new added property using react and javascript?

Hi i have array of objects like below,

const arr_obj = [ 
    {
        children: [{}],
        type: "type1",
        updated: "somevalue",
    },
    {
         children: [{}],
         type: "type2",
         updated: "somevalue",
     },
]

now i want to create a new array of object that will have each object as in arr_obj and also add new property "disabled" which is boolean value set to true if type is equal to "type2" if not set to false.

so the expected output is like below

const new_obj = [
    {
        children: [{}],
        type: "type1",
        updated: "somevalue",
        disabled: false,
    },
    {
        children: [{}],
        type: "type2",
        updated: "somevalue",
        disabled: true,
    },
]
    

I have tried like below

const new_obj = React.useMemo(() => {
    arr_obj.map((arr => ({
        ...arr,
        disabled: arr?.type !== "type2" ? false : true,
    }))
}, [arr_obj]
);

but when i log new_obj this is undefined. could someone help me with this. I am new to programming. thanks.

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

0

You're not returning the result of arr_obj.map so the function returns undefined.

Either

const new_obj = React.useMemo(() => {
  return arr_obj.map((arr) => ({
    ...arr,
    disabled: arr?.type !== "type2" ? false : true,
  }));
}, [arr_obj]);

or drop the braces:

const new_obj = React.useMemo(
  () =>
    arr_obj.map((arr) => ({
      ...arr,
      disabled: arr?.type !== "type2" ? false : true,
    })),
  [arr_obj],
);
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