Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
Getting values from json in JavaScript

If I have some json like this:

{
    "dcf93893d83e4f28953f140b9cba1963":{
       "foo":{
          "client":"127.0.0.1",
          "id":"31971",
          "start time":1654131724.160335
       },
       "bar":{
          "client":"127.0.0.1",
          "id":"23456",
          "start time":1654131900.997766
       }
    }
 }

I figured out how to loop through it like this:

for (var key in json) {
    if (json.hasOwnProperty(key)) {
        console.log(json[key])
    }
}

How can I loop through each sub element (foo and bar in this case) and then fetch a key, for example id?

client, id and start time are the only known key names

Expected output:

31971
23456

EDIT: The json comes from fetch:

setInterval(function () {
    fetch(<url>)
        .then(function (response) {
            return response.json();
        })
        .then(function (json) {
            for (var key in json) {
                if (json.hasOwnProperty(key)) {
                    console.log(json[key])
                }
            }
        })
}, 2000);
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Do another loop and grab the inner values by key name:

const json = {
    "dcf93893d83e4f28953f140b9cba1963":{
       "foo":{
          "client":"127.0.0.1",
          "id":"31971",
          "start time":1654131724.160335
       },
       "bar":{
          "client":"127.0.0.1",
          "id":"23456",
          "start time":1654131900.997766
       }
    }
 }
 
for (var key in json) {
    if (json.hasOwnProperty(key)) {
        //console.log(json[key])
        
        for(var key2 in json[key]) {
          console.log(json[key][key2]["id"])
        }
    }
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have to loop in each loop. and save each id into an array.

var ids = []
for (var key in json) {
    if (json.hasOwnProperty(key)) {
        for(var obj in json[key]) {
           if (obj.id) ids.push(obj.id)
        }
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Instead of iterating, you could just get the object's values with Object.values(), and map those accordingly using flatMap() and map():

const data = {
  "dcf93893d83e4f28953f140b9cba1963": {
    "foo": {
      "client": "127.0.0.1",
      "id": "31971",
      "start time": 1654131724.160335
    },
    "bar": {
      "client": "127.0.0.1",
      "id": "23456",
      "start time": 1654131900.997766
    }
  }
};

const result = Object.values(data)
                     .flatMap(v => Object.values(v)).map(({id}) => id);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda