Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

javascript - 如果使用过滤器找不到牙医,则返回null(Return null if no dentist found using filter)

I have the following code:(我有以下代码:)

function findFirstDentist(people) { let firstDentistFound = people.filter(dentist => person.isDentist) } This is been run against:(这是针对:) const dentists = [ { name: "Johnny Karate", isDentist: false }, { name: "Lucy Hobbs Taylor", isDentist: true }, { name: "Pierre Fauchard", isDentist: true }, { name: "Bert Macklin", isDentist: false }, { name: "Orin Scrivello", isDentist: true }, { name: "Kip Hackman", isDentist: false } ]; I am struggling to make this return null if there are no dentists showing true.(如果没有牙医证明我是真的,我正在努力使此返回为空。)   ask by JJSR23 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could check the length of the new array and return either the array or null .(您可以检查新数组的length ,然后返回array或null 。)

function findFirstDentist(people) { let firstDentistFound = people.filter(dentist => dentist.isDentist); return firstDentistFound.length ? firstDentistFound : null; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...