Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

259
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda