“Count where” in a collection

Solution

You can use countBy:

const total = _.countBy(
    array,
    ({ a }) => a < 4 ? 'lessThanFour' : 'greaterThanFour'
  ).lessThanFour

Alternative

Using sumBy:

const total = _.sumBy(
  array,
  ({ a }) => Number(a < 4)
);

And here’s the same but with lodash/fp:

const count = _.sumBy(_.flow(_.get('a'), _.lt(4), Number), objects);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)