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

207
Visualizações
Read data from Json and store in variable javascript

I am getting a response of an api request in JSON format which I store in a variable with json data like this structure

const users=    {
    'ABC': {
        id: '123',
        name:'testname',
    },
    'DEF': {
        id = '222',
        name='testname2'
    }
}

I am trying to read each data under users and store in a different variable. One variable lets say name firstVar which will have all date under 'ABC' and secondVar which will have all data under'DEF' which i can then use for further tests. I am not sure how to achieve it. Any help will be appreciable.

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

0

If you want to get the ids of each object and store them you can map over the Object.values and return an array of ids.

const users = {
  'ABC': {
      id: '123'
  },
  'DEF': {
      id: '222'
  }
};

const ids = Object.values(users).map(el => {
  return el.id;
});

console.log(ids);

about 4 years ago · Juan Pablo Isaza Relatório

0

Not sure if this is what you want, your code is cut off. Array.prototype.find function only works for Array.

const users = {
    'ABC': {
        id: '123'
    },
    'DEF': {
        id: '222'
    }
};

const foundId = Object.keys(users).find(username => users[username].id === '123');

users[foundId] // found user.
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