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

190
Visualizações
Best Method to Grab Specific Keys(Properties) from Nested Object in JavaScript

What is the best method to get specific keys (i.e. "longName") from nested objects with this structure:

const routes = {
   "14094": {
      "routeId": 14094,
      "shortName": "2",
      "longName": "Route 1"
   },
   "14095": {
      "routeId": 14095,
      "shortName": "7",
      "longName": "Route 2"
   },
   "14096": {
      "routeId": 14096,
      "shortName": "42",
      "longName": "Route 3"
   },
   "14097": {
      "routeId": 14097,
      "shortName": "57",
      "longName": "Route 4"
   },

I have tried to console.log(routes[1]), dot notation, and other methods but I am not having any success.

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

0

The structure here is an object of objects. So you can access the longName key-value pair in this manner: routes[14094].longName which should return Route 1, based on the original data.

If you want to get all the longName values for each object, you can use a for loop like this:

for (const key in routes) {
   console.log(routes[key].longName)
   }

You need to first reference the object using the "outer" key (e.g. 14094), then you can use dot notation for the keys in the "inner" object.

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