1

So starting from an array of objects I need to find if any of them are called Google. If so I need to return the full object. Now It is returning true

const companies = [ { id: 1, username: 'facebook', website: 'www.facebook.com' }, { id: 2, username: 'google', website: 'www.google.com' }, { id: 3, username: 'linkedin', website: 'www.linkedin.com' } ]
const checkCompany = company => company.name === "google";

console.log(companies.some(checkCompany)); // true
Markus Hayner
  • 2,869
  • 2
  • 28
  • 60
  • 2
    does it work? your objects hav different properties than in the callback. do you have only one object that matches? – Nina Scholz Mar 26 '19 at 15:24

1 Answers1

5

some() returns a Boolean.You can use Array.prototype.find() which returns the value of first element in array. If no element in array matches condition then it returns undefined

const companies = [ { id: 1, username: 'facebook', website: 'www.facebook.com' }, { id: 2, username: 'google', website: 'www.google.com' }, { id: 3, username: 'linkedin', website: 'www.linkedin.com' } ]
const res = companies.find(x => x.username === "google");
console.log(res)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73