Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 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 Answer

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

2.1m questions

2.1m answers

60 comments

57.0k users

...