0

I have a list with the following values:

const myList = [True, False, False]

With myList.length I get the value 3.

Now I want the length of the list with all False values.

How can I return the length with a condition?

JohnDole
  • 525
  • 5
  • 16

3 Answers3

3
const myList = [True, False, False].filter(el => el === false)
console.log(myList.length)
voidbrain
  • 351
  • 1
  • 3
  • 13
1

You can use Array.filter() to do it

let len = myList.filter(i => !i).length

const myList1 = [true, false, false]
let len1 = myList1.filter(i => !i).length
console.log(len1)

const myList2 = ['True', 'False', 'False']
let len2 = myList2.map(i => i.toLowerCase() === 'true').filter(i => !i).length
console.log(len2)
flyingfox
  • 13,414
  • 3
  • 24
  • 39
1

You could count the values.

const
    True = true,
    False = false,
    getCount = (array, value) => array.reduce((t, v) => t + (v === value), 0),
    myList = [True, False, False];

console.log(getCount(myList, False));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392