Home > database >  How to find If array of objects includes an element in javascript? and How to access it if includes?
How to find If array of objects includes an element in javascript? and How to access it if includes?

Time:09-26

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

CodePudding user response:

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)

CodePudding user response:

searchEngines.name.includes('bing') throws an error because searchEngines is an array and therefore has no name property.

You had the right idea though, you just need to use the map method to get a list of just the names, rather than a list of objects, and then use includes to test that.

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

const searchEngineNames = searchEngines.map(engine => engine.name);

console.log(searchEngineNames.includes('bing'));

CodePudding user response:

var searchEngines = [{
    name: 'google',
    link: 'https://google.com'
  },
  {
    name: 'bing',
    link: 'https://bing.com'
  }];
  
const result = searchEngines.reduce((acc, {name, link}) =>{
  acc['name'] = acc['name'] ? [...acc['name'], name] : [name];
  acc['link'] = acc['link'] ? [...acc['link'], link] : [link];
  return acc;
}, {});

// search by name
const searchName = 'bing';
const index = result.name.findIndex(x => x==searchName);
if(index!=-1){
  console.log(searchEngines[index].link)
}

// search by link
const searchLink = 'https://google.com';
const index2 = result.link.findIndex(x => x==searchLink);
if(index2!=-1){
  console.log(searchEngines[index2].name)
}

CodePudding user response:

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)
}

  • Related