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

88
Vistas
Is it possible to search for a specific element in a json script with a variable

The following script is an example script. I need it to log the name Joe;

const names = `

    {
         "Joe": {
              "nat": "American",
              "hair": "brown"
         },
         "Peter": {
              "nat": "German",
              "hair": "blond"
         }
    }
`;

const load = JSON.parse(names);

var search = "Joe"

console.log(names.search);

The last line outputs "undefined" because it's searching for "search" instead of the value of the variable search. Before you ask: yes, I need it to be a variable (because the value of the variable would actually be the value of an input). Is there any way I can search the value of a variable in the JSON bit?

Thanks in advance

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Almost there (you still have to parse it first):

const names = `

    {
         "Joe": {
              "nat": "American",
              "hair": "brown"
         },
         "Peter": {
              "nat": "German",
              "hair": "blond"
         }
    }
`;

const load = JSON.parse(names);

var search = "Joe"

console.log(load[search]);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try using names[search] instead

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