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

293
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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