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

207
Vistas
Extracting values out of an array of objects?

I am trying to extract id from the below array of objects and so far I am able to give it a go with the below code but it is showing undefined and cannot get id , would you please check the code and adjust to get id out?

const array = [{
    contact: {
      id: 1,
      email: 'roar@gmail.com',

    },
    date: '2/4/22'
  },
  {
    contact: {
      id: 2,
      email: 'grr@gmail.com',

    },
    date: '2/4/22'
  }
]

function extractValue(arr, prop) {

  let extractedValue = [];

  for (let i = 0; i < arr.length; ++i) {
    // extract value from property
    extractedValue.push(arr[i][prop]);
  }
  return extractedValue;
}


const result = extractValue(array, 'contact.id');
console.log(result);

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

0

const extractValue = (array, path) => {
  const [first, second] = path.split('.')
  if (second) return array.map(obj => obj[first][second])
  
  return array.map(obj => obj[first])
}

const res = extractValue(array, 'contact.id')
console.log(res)
// => [ 1, 2 ]

this will support single and double level nested results

about 4 years ago · Juan Pablo Isaza Denunciar

0

A good way to do this is the Array Map method

This will get all the id's from your array

const result = array.map((val) => val.contact.id)
about 4 years ago · Juan Pablo Isaza Denunciar

0

function find(val, arr) {  
  for (let x of arr)
    val = val[x];
  return val;  
}
function extractValue(arr, prop) {
 return array.map(x => find(x, prop.split(".")))
}
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