Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

Categories

0 votes
122 views
in Technique[技术] by (31.9m 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

Please log in or register to answer this question.

1 Answer

0 votes
by (31.9m 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 Qi-U Community for programmer and developer-Open, Learning and Share
...