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
1.3k views
in Technique[技术] by (71.8m points)

javascript - How to check two conditions in filter?

I have an array,

const array = [
{
   name: G1,
   male: true,
   female: false
},
{
   name: G2,
   male: false,
   female: true
}]

Now in this, i need to filter or fetch name=G1, and then i need to check if male is true or female is true ? And the output as Male or Female ?

I tried ;

array.filter(e => e.name === "G1" && (e.malesSeat === true || e.ladiesSeat === true))

This gives, result but how to know which is true as Male or Female ?

Expected Result:

Male === true or just Male

As for G1, only Male is true!


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

1 Reply

0 votes
by (71.8m points)

Check the code. Just use male or female.

const array = [
{
   name: 'G1',
   male: true,
   female: false
},
{
   name: 'G2',
   male: false,
   female: true
}]

const arr = array.filter(e => e.name === "G1" && (e.male || e.female))
   console.log(arr.length >= 1 ? (arr[0].male ? 'Male' : 'Female') : 'Not found')

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

...