Как посчитать количество элементов?

Как в JavaScript можно посчитать количество элементов в Journal где type = 2

[
  {
    "id": 1,
    "name": "Алексей",
    "journal": [
      {
        "id": 41,
        "user_id": 1,
        "schedule": "2021-03-07",
        "type": 2,
        "created_at": "2021-03-01T12:33:33.000000Z",
        "updated_at": "2021-03-01T12:33:38.000000Z"
      },
      {
        "id": 42,
        "user_id": 1,
        "schedule": "2021-03-06",
        "type": 2,
        "created_at": "2021-03-01T12:33:51.000000Z",
        "updated_at": "2021-03-01T12:33:51.000000Z"
      },
      {
        "id": 43,
        "user_id": 1,
        "schedule": "2021-03-14",
        "type": 2,
        "created_at": "2021-03-01T12:34:42.000000Z",
        "updated_at": "2021-03-01T12:34:42.000000Z"
      },
      {
        "id": 44,
        "user_id": 1,
        "schedule": "2021-03-21",
        "type": 1,
        "created_at": "2021-03-01T12:34:45.000000Z",
        "updated_at": "2021-03-01T12:34:45.000000Z"
      },
      {
        "id": 45,
        "user_id": 1,
        "schedule": "2021-03-27",
        "type": 1,
        "created_at": "2021-03-01T12:34:47.000000Z",
        "updated_at": "2021-03-01T12:34:47.000000Z"
      },
      {
        "id": 46,
        "user_id": 1,
        "schedule": "2021-03-28",
        "type": 1,
        "created_at": "2021-03-01T12:34:48.000000Z",
        "updated_at": "2021-03-01T12:34:48.000000Z"
      },
      {
        "id": 47,
        "user_id": 1,
        "schedule": "2021-03-08",
        "type": 1,
        "created_at": "2021-03-01T12:34:57.000000Z",
        "updated_at": "2021-03-01T12:34:57.000000Z"
      }
    ]
  }
]

Ответы (1 шт):

Автор решения: Aziz Umarov

Немного усложнив для наглядности пример можно получить так

let array = [
  {
    "id": 1,
    "name": "Алексей",
    "journal": [
      {
        "id": 41,
        "user_id": 1,
        "schedule": "2021-03-07",
        "type": 2,
        "created_at": "2021-03-01T12:33:33.000000Z",
        "updated_at": "2021-03-01T12:33:38.000000Z"
      },
      {
        "id": 42,
        "user_id": 1,
        "schedule": "2021-03-06",
        "type": 2,
        "created_at": "2021-03-01T12:33:51.000000Z",
        "updated_at": "2021-03-01T12:33:51.000000Z"
      },
      {
        "id": 43,
        "user_id": 1,
        "schedule": "2021-03-14",
        "type": 2,
        "created_at": "2021-03-01T12:34:42.000000Z",
        "updated_at": "2021-03-01T12:34:42.000000Z"
      },
      {
        "id": 44,
        "user_id": 1,
        "schedule": "2021-03-21",
        "type": 1,
        "created_at": "2021-03-01T12:34:45.000000Z",
        "updated_at": "2021-03-01T12:34:45.000000Z"
      },
      {
        "id": 45,
        "user_id": 1,
        "schedule": "2021-03-27",
        "type": 1,
        "created_at": "2021-03-01T12:34:47.000000Z",
        "updated_at": "2021-03-01T12:34:47.000000Z"
      },
      {
        "id": 46,
        "user_id": 1,
        "schedule": "2021-03-28",
        "type": 1,
        "created_at": "2021-03-01T12:34:48.000000Z",
        "updated_at": "2021-03-01T12:34:48.000000Z"
      },
      {
        "id": 47,
        "user_id": 1,
        "schedule": "2021-03-08",
        "type": 1,
        "created_at": "2021-03-01T12:34:57.000000Z",
        "updated_at": "2021-03-01T12:34:57.000000Z"
      }
    ]
  },
  {
    "id": 2,
    "name": "Николай",
    "journal": [
      {
        "id": 41,
        "user_id": 2,
        "schedule": "2021-03-07",
        "type": 2,
        "created_at": "2021-03-01T12:33:33.000000Z",
        "updated_at": "2021-03-01T12:33:38.000000Z"
      },
      {
        "id": 42,
        "user_id": 2,
        "schedule": "2021-03-06",
        "type": 2,
        "created_at": "2021-03-01T12:33:51.000000Z",
        "updated_at": "2021-03-01T12:33:51.000000Z"
      },
      {
        "id": 43,
        "user_id": 2,
        "schedule": "2021-03-14",
        "type": 2,
        "created_at": "2021-03-01T12:34:42.000000Z",
        "updated_at": "2021-03-01T12:34:42.000000Z"
      },
      {
        "id": 44,
        "user_id": 2,
        "schedule": "2021-03-21",
        "type": 1,
        "created_at": "2021-03-01T12:34:45.000000Z",
        "updated_at": "2021-03-01T12:34:45.000000Z"
      },
      {
        "id": 45,
        "user_id": 2,
        "schedule": "2021-03-27",
        "type": 1,
        "created_at": "2021-03-01T12:34:47.000000Z",
        "updated_at": "2021-03-01T12:34:47.000000Z"
      },
      {
        "id": 46,
        "user_id": 2,
        "schedule": "2021-03-28",
        "type": 1,
        "created_at": "2021-03-01T12:34:48.000000Z",
        "updated_at": "2021-03-01T12:34:48.000000Z"
      },
      {
        "id": 47,
        "user_id": 2,
        "schedule": "2021-03-08",
        "type": 1,
        "created_at": "2021-03-01T12:34:57.000000Z",
        "updated_at": "2021-03-01T12:34:57.000000Z"
      }
    ]
  }
]

let array2 =  array.flatMap(item=>item.journal).filter(jurnal=>jurnal.type === 2);

console.log(`массив длина ${array2.length} , сам массив = `,  array2)

→ Ссылка