JavaScript: How to count the number of objects with a certain value

Let’s say we have an array of people and we want to find out how many people are older than 30.

A simple way would be filtering the array first and then get its length.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
const people = [
    {
        id: 1,
        age: 15
    },
    {
        id: 2,
        age: 20
    },
        {
        id: 3,
        age: 40
    },
        {
        id: 4,
        age: 50
    },
]

const count = people.filter(x => x.age > 30).length

console.log(count) // 2