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
Run Code Online (Sandbox Code Playgroud)
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)Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50 次 |
| 最近记录: |