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

120
Visualizações
Find object by key in array of object

Hello this is array of objects

  const raceId = '1004'
  const array =  [
    {
      '1004': { brackets: { '18': [Object], '19': [Object], '20': [Object] } }
    },
    {
      '999': { brackets: { '1': [Object], '2': [Object], '3': [Object] } }
    }
  ] 

So i am trying to return just a

{ brackets: { '18': [Object], '19': [Object], '20': [Object] } }

I have tried to use reduce(), map(), find() but not getting desired output. For example i have tried.

const reduced = raceScoringConfigGroups.reduce((acc, key) => {
   if (Object.keys(key)[0] === raceId) {
     return key[raceId]
   }
  }, {})

But for some reason getting undefined, can someone help me just a little bit, not sure what i am doing wrong. I don't like solutions with forEach or for loops, trying to write oneliner.

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

0

The value you return in the reduce() callback is used as the accumulator in the next iteration. Since you don't return anything when the key doesn't match raceId, your code will only work if the matching key is the last element of the array.

Use find() rather than reduce(), and then get the property from that.

const found = raceScoringConfigGroups.find(el => el[raceId]);
const reduced = found?.[raceId];
about 4 years ago · Juan Pablo Isaza Relatório

0

Can you try this?

const result = array.filter(el => Object.keys(el) === raceId)[0];

about 4 years ago · Juan Pablo Isaza Relatório

0

Well to a certain extent it is working as a loop because if you put a console.log before the return key[raceId] you will see that it first returns a value and then undefined.

I don't know if it's close to what you want to do but this is a way to find the object too

const findIt = array.find( item => Object.keys(item)[0] === raceId);
console.log(findIt)
console.log(findIt['1004'])
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