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

252
Vistas
How to find If array of objects includes an element in javascript? and How to access it if includes?

How can I find if an array of objects includes an element?

Suppose code is

var searchEngines = [{
    name: 'google',
    link: 'https://google.com'
  },
  {
    name: 'bing',
    link: 'https://bing.com'
  }];

//now I want to check if bing includes in array searchEngines or not (below code is wrong, please correct it)
console.log(searchEngines.name.includes('bing'));
//must return true. but it doesn't say true instead gives me an error and crashes the app.

so how can I check if searchEngines array includes bing or not? Now if successfully checked that bing is included in searchEngines array, now how can I access bing?

like I wanna get the index number of bing array inside searchEngines array and then grab the link of it and console.log it, so I'll get https://bing.com

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

0

in this line console.log(searchEngines.name.includes('bing'));, you're trying to access the name property of the searchEngines variable which is an array.

I think a better way to find whether the array includes an object with the name variable set to 'bing' would be to use the find() function.

Here's an example:

var searchEngines = [{
    name: 'google',
    link: 'https://google.com'
  },
  {
    name: 'bing',
    link: 'https://bing.com'
  }];
  
const includesBing = searchEngines.find(se => se.name === 'bing') !== undefined;
const includesYahoo = searchEngines.find(se => se.name === 'yahoo') !== undefined;

console.log(includesBing)
console.log(includesYahoo)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Array.find will return undefined if no item which matches the condition is found.

You can use it to find the first item whose name property is 'bing'. To check whether an item was found, coerce it to a boolean and check whether it is true. If so, the item exists, and you can access it:

var searchEngines = [{
    name: 'google',
    link: 'https://google.com'
  },
  {
    name: 'bing',
    link: 'https://bing.com'
  }
];

const includesBing = searchEngines.find(e => e.name == 'bing')

if(includesBing){
  // bing exists
  console.log(includesBing.link)
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

I personally like simple solutions that also work for older versions.

var searchEngines = [{
    name: 'google',
    link: 'https://google.com'
  },
  {
    name: 'bing',
    link: 'https://bing.com'
  }];

function itemExists(propName, searchValue) {
    for (var i in searchEngines) {
        if (searchEngines[i][propName] === searchValue) {
            return true;
        }
    }
    return false;
}

console.log("containes 'bing': " + itemExists("name", "bing"));
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