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

289
Vistas
How to get values from array of objects in javascript

I have an array of object from which I am trying to get values using map operator but I am getting the whole json objects all I want is just array of values. Below is my code:

const obj = [
             {
              a: {
                  b: 'Paul',
                 }
             },
              {  
                c: 'Byeeee',
              }
           ];

obj.map((val) => console.log(val));  

what I am getting is

{ a: { b: 'Paul' } }
{ c: 'Byeeee' }

What I want is:

['Paul','Byeeee']

Someone let me know how can I get the desired output.

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

0

You can do this recursively. You can first start off by grabbing the values of your object, and then loop through those using .flatMap(). If you encounter a value that is an object, you can recursively grab the values of that object by recalling your function. Otherwise, you can return the value. The advantage of using .flatMap() here is that when the recursive call returns an array, we don't end up with inner arrays, but rather the array gets flattened into one resulting array:

const obj = [{ a: { b: 'Paul', } }, { c: 'Byeeee', } ];

const getValues = (obj) => {
  return Object.values(obj).flatMap(val => Object(val) === val ? getValues(val) : val);
}
console.log(getValues(obj));

about 4 years ago · Juan Pablo Isaza Denunciar

0

you can use the following solution.

  const data = [{ a: { b: 'Paul' } }, { c: 'Byeeee' }];

  const flatObjectValues = (obj, result) => {
  // recursive function to get object values
  const objValues = Object.values(obj);
  if (objValues?.length > 0) {
    objValues.map((v) => {
      if (typeof v === 'object' && !Array.isArray(v)) {
        flatObjectValues(v, result);
      } else {
        result.push(v);
      }
      return v;
    });
  }
};

const updatedData = [];
data.map((x) => flatObjectValues(x, updatedData));
console.log('updatedData: ', updatedData);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use recursion with array.reduce, like fellowing.

function getAllValues(objuct) {
  return objuct.reduce((acc, curr) => {
    if (typeof curr === 'object') {
      return [...acc, ...getAllValues(Object.values(curr))];
    }
    return [...acc, curr];
  }, []);
}
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