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

252
Visualizações
What is the best way to access key/values of a object array when I don't know them?

I have this array above and I need every property of it

let arr = [{'John': 0}, {'Doe': 50}, {'Marry': 100}]

How could I extract every single key/value of it, once in theory, I don't know any of them? I have already tried using object.keys but it returns the indexes of my array.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This should work

const arr = [{'John': 0}, {'Doe': 50}, {'Marry': 100}];


// to iterate over each element in the arry  
arr.forEach(a => {
    // To Iterate over each key in the element object
    Object.keys(a).forEach(k => {
      // to print the value of the 'k' key
      console.log(k + ' : ' + a[k]);
  })
})

about 4 years ago · Juan Pablo Isaza Relatório

0

1) You can use flatMap and Object.keys to get keys from an array of objects.

let arr = [{ John: 0 }, { Doe: 50 }, { Marry: 100 }];

const result = arr.flatMap((o) => Object.keys(o));
console.log(result);

2) To find all values in an array

let arr = [{ John: 0 }, { Doe: 50 }, { Marry: 100 }];

const values = arr.flatMap((o) => Object.values(o));
console.log(values);

3) If you want to find out all keys and values in an object

let arr = [{ John: 0 }, { Doe: 50 }, { Marry: 100 }];

const result = {
  keys: [],
  values: [],
};

for (let obj of arr) {
  Object.entries(obj).map(([k, v]) => {
    result.keys.push(k);
    result.values.push(v);
  });
}

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want to collect all the keys and values of a nested array of objects, you can use Array.prototype.reduce and then collect the keys and values of the nested objects in separate nested arrays, using Object.keys() and Object.values() respectively:

const arr = [{'John': 0}, {'Doe': 50}, {'Marry': 100}];

const allKeysAndValues = arr.reduce((acc, cur) => {
  acc.keys.push(...Object.keys(cur));
  acc.values.push(...Object.values(cur));
  return acc;
}, { keys: [], values: [] });

console.log(allKeysAndValues);

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