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

113
Vistas
Reading a non static key from a JSON file in Javascript

I'm trying to read the current profile of a user from an API, but the problem is that I would not known the profile id, since it is randomly generated. Is there a way to maybe skip this key and check which object has current set to true, or maybe another way?

Any help is appreciated.

{
    "profiles": {
        "70560fe0cdce4e9b91a0a161a257e188": {
            "profile_id": "70560fe0cdce4e9b91a0a161a257e188",
            "cute_name": "Pomegranate",
            "current": true,
            "last_save": 1632932207555,
            "raw": {},
            "items": {},
            "data": {}
        },
        "b49bab4a08a64be38edec3f9ecf8f639": {
            "profile_id": "b49bab4a08a64be38edec3f9ecf8f639",
            "cute_name": "Raspberry",
            "current": false,
            "last_save": 1630180158660,
            "raw": {},
            "items": {},
            "data": {}
        }
    }
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can loop through an object's enumerable properties, such as these, without needing to know their keys by using a for...in loop.

Otherwise, depending on what browsers you have to support, you could use Object.values to convert an object into an array of its enumerable values, and you could then run Array.prototype.filter.

Here's a brief example using a for...in loop, assuming your API response is saved in a data variable:

let activeProfiles = [];

for (let profileId in data.profiles) {
    let profile = data.profiles[profileId];
    if (profile.current === true) {
        activeProfiles.push(profile);
    }
}

And if you wanted to use the Object.values method, something like this should do the trick:

const activeProfiles = Object.values(data.profiles).filter((profile) => profile.current === true);
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you're using modern JS you can filter the objects to find what you need.

const obj = {
  "profiles": {
    "70560fe0cdce4e9b91a0a161a257e188": {
      "profile_id": "70560fe0cdce4e9b91a0a161a257e188",
      "cute_name": "Pomegranate",
      "current": true,
      "last_save": 1632932207555,
      "raw": {},
      "items": {},
      "data": {}
    },
    "b49bab4a08a64be38edec3f9ecf8f639": {
      "profile_id": "b49bab4a08a64be38edec3f9ecf8f639",
      "cute_name": "Raspberry",
      "current": false,
      "last_save": 1630180158660,
      "raw": {},
      "items": {},
      "data": {}
    }
  }
}

const profiles = Object.values(obj.profiles) // convert to useable array

console.log(profiles.filter(p => p.current === true))

about 4 years ago · Juan Pablo Isaza Denunciar

0

const data = {
    "profiles": {
        ...
    }
}

for (const [key, value] of Object.entries(data.profiles)) {
  if (value.current) {
      console.log(`${key} is true`);
  }
}

Output: 70560fe0cdce4e9b91a0a161a257e188 is true

That could be an easy approach to work with the data

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