Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!