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

100
Vistas
Returning a particular property of an object from an array of objects

My array of objects looks something like this

const arr1 = [
  {person: {isPresent: true, isInsured: true, value:20}},
  {status: {isPresent: true, isInsured: true, value:20}},
]

I want to return an object/array that looks something like this

[
 {person: {value:20}},
 {status: {value:20}},
]

I tried mapping this array but I am unable to get the desired result and I am wondering what am I missing. Any help appreciated, thanks!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You might need something like this:

interface Details {
  isPresent: boolean;
  isInsured: boolean;
  value: number;
}

interface Person {
  person: Details;
}

interface Status {
  status: Details;
}

type Tuple = [Person, Status];

const arr1: Tuple = [
  { person: { isPresent: true, isInsured: true, value: 20 } },
  { status: { isPresent: true, isInsured: true, value: 20 } },
];

const extractValue = ({ value }: Details) => ({ value });

const result = arr1.map(x => {
  if ('person' in x) {
    return { person: extractValue(x.person) };
  }
  if ('status' in x) {
    return { status: extractValue(x.status) };
  }
  return x;
});

console.log(result);

You can play with it in this TypeScript playground

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do this:

const arr1 = [
    {person: {isPresent: true, isInsured: true, value:20}},
    {status: {isPresent: true, isInsured: true, value:20}},
];

const newArray = [];


for (const val in arr1) {

    const keys = Object.keys(arr1[val]);
    keys.forEach((key, index) => {
        //console.log(key);
        //console.log(arr1[val][key]['value']);
        newArray.push({[key]: {value: arr1[val][key]['value']}});

    });
}
console.log(newArray);

Will return:

[ { person: { value: 20 } }, { status: { value: 20 } } ]
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