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

120
Vistas
Get object property value from list of objects without for loop

I have an array of objects in JS

myArray = [
 {name: 'Jim', id: "2"}
 {name: 'Pam', id: "10"}
 {name: 'Dwight', id: "7"}
];

Given I have

var currentID = 2;

And I want to get the name from that array of objects where the id = 2 (all objects will have a unique ID field so there is no issue with multiple objects with the same ID) is there a better way than just doing this

testFunction() {
    var myArray = [
      { name: "Jim", id: "2" },
      { name: "Pam", id: "10" },
      { name: "Dwight", id: "7" },
    ];
    var currentID = "10";
    var currentName = "";

    for (let i in myArray) {
      console.log(i);
      if (myArray[i].id == currentID) {
        currentName = myArray[i].name;
        console.log(currentName);
        break;
      }
    }
  }

This will output "Pam" but i wanted to know if there is a more elegant way to index into that object without the need of a for loop? If not its not too much of an issue as I'm mostly just looking to clean up my code. Thanks!

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