Counting the occurrences / frequency of array elements

前端 未结 30 2131
甜味超标
甜味超标 2020-11-21 06:47

In Javascript, I\'m trying to take an initial array of number values and count the elements inside it. Ideally, the result would be two new arrays, the first specifying each

30条回答
  •  梦谈多话
    2020-11-21 07:19

    Using Lodash

    const values = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
    const frequency = _.map(_.groupBy(values), val => ({ value: val[0], frequency: val.length }));
    console.log(frequency);

提交回复
热议问题