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

522
Vistas
JavaScript - How can I reference an item in JSON like variable?

Consider I have the following variable:

var result = {
    "Items": [
      {
        "location": "New York",
        "visible": false,
        "destinations": [
          4,
          3,
          5
        ],
        "id": 3,
        "coordinates": {
          "lng": -74.17,
          "lat": 40.68
        }
      },{
        "location": "Madrid",
        "visible": false,
        "destinations": [
          0,
          4
        ],
        "id": 0,
        "coordinates": {
          "lng": -3.56,
          "lat": 40.49
        }
      },
      {
        "location": "Los Angeles",
        "visible": false,
        "destinations": [
          4,
          3,
          5
        ],
        "id": 5,
        "coordinates": {
          "lng": -118.4,
          "lat": 33.94
        }
      }
    ]
  };

If I want to reference to Los Angeles, I would do as below and it works great:

  console.log(result.Items[2]);

What if I don't know the order but I know its id (5). How can I reference it by its ID?

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

You can't reference it directly, but you can use some Javascript function on the array to find it, such as:

result.Items.find(x => x.id == 5);

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

about 4 years ago · Santiago Gelvez Denunciar

0

Filter your input like that

let newFilteredArray = result.filter(item => {
    return item.id === 5;
};
console.log(newFilteredArray[0];
about 4 years ago · Santiago Gelvez Denunciar

0

You can do:

    var result = {
        "Items": [
            {
                "location": "New York",
                "visible": false,
                "destinations": [
                    4,
                    3,
                    5
                ],
                "id": 3,
                "coordinates": {
                    "lng": -74.17,
                    "lat": 40.68
                }
            }, {
                "location": "Madrid",
                "visible": false,
                "destinations": [
                    0,
                    4
                ],
                "id": 0,
                "coordinates": {
                    "lng": -3.56,
                    "lat": 40.49
                }
            },
            {
                "location": "Los Angeles",
                "visible": false,
                "destinations": [
                    4,
                    3,
                    5
                ],
                "id": 5,
                "coordinates": {
                    "lng": -118.4,
                    "lat": 33.94
                }
            }
        ]
    };
    
    
    var array = JSON.parse(JSON.stringify(result));
    
    for (i = 0; i <= array.Items.length - 1; i++) {
        if (array.Items[i].id == 5) {
            console.log(array.Items[i])
        }
    }

about 4 years ago · Santiago Gelvez 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