Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

111
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda