因此,从一组对象开始,我需要确定它们中的任何一个是否称为Google。如果是这样,我需要返回完整的对象。现在它正在返回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
答案 0 :(得分:5)
some()
返回一个Boolean
。您可以使用Array.prototype.find()
返回数组中第一个元素的值。如果数组中没有元素符合条件,则返回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)