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

180
Vistas
Get json dynamic key in JS

I need to get the key & value from a JSON to add it in an option created in JS.

In my html, I've just add input hidden to get data in JS like this :

<input type="hidden" value="<?= htmlspecialchars(json_encode($availableFlag)) ?>" id="list-flag">

$availableFlag is defined in my php just above :

$availableFlag = App::getAvailableTags();

And this function is defined in another class and give me that :

public static function getAvailableTags(): array {
        return array_merge(self::$availableTags, [
            Tag::TAG_OUT_UE => Tag::TAG_TYPE_ERROR, // Those variables are const
            Tag::TAG_PHYSICAL_TICKET     => Tag::TAG_TYPE_WARNING, // defined in the Tag class
        ]);
    }

So, back to my main problem. I'm creating dynamic options based on the lenght of my array

const listFlag = JSON.parse(oid("list-flag").value);
        
for (let i = 0; i < listFlag.length; i++) {
    filterFlagSelect.options[filterFlagSelect.options.length] = new Option(listFlag[i], listFlag[i]);
}

But listFlag[i] is undefined, and I don't know what to do because keys in listFlag is never the same.

This is my content in listFlag

{
  "out-ue": "error",
  "physical-ticket": "warning"
}

And listFlag.length seem to have problems too

about 4 years ago · Juan Pablo Isaza
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