mparkitny
mparkitny

Reputation: 1195

Count elements per day Javascript

I have array of objects like below.

[
  {
    name: 'order-1',
    date: '2017-05-30 16:38:30.418Z'
  },
  {
    name: 'order-2',
    date: '2017-05-30 16:38:30.418Z'
  },
  {
    name: 'order-3',
    date: '2017-05-31 16:38:30.418Z'
  }
]

I would like to count how many objects was every day and create an array like so: [1, 2].

I was able to group records by days using underscore.js and moment library

_.groupBy(orders, function order) {
  return moment(order.created).startOf('day').format();
});

How can I count below elements?

[
  {
    "2017-05-30T00:00:00+02:00": [
      {
        name: 'order-1',
        date: '2017-05-30 16:38:30.418Z'
      },
      {
       name: 'order-2',
       date: '2017-05-30 16:38:30.418Z'
       }
     ]
    },
    {
      "2017-05-30T00:00:00+02:00": [
        {
          name: 'order-3',
          date: '2017-05-31 16:38:30.418Z'
        }
      ]
    }
  }
]

Upvotes: 1

Views: 1322

Answers (3)

Adam Paxton
Adam Paxton

Reputation: 1432

Maybe like this:

var orders = [
    {
        name: 'order-1',
        date: '2017-05-30 16:38:30.418Z'
    },
    {
        name: 'order-2',
        date: '2017-05-30 16:38:30.418Z'
    },
    {
        name: 'order-3',
        date: '2017-05-31 16:38:30.418Z'
    }
];
var res = _.chain(orders)
.groupBy(function(order) {
    return moment(order.date).startOf('day').format();
})
.map(function(obj){
    return obj.length;
})
.value();


console.log(res); //[ 2, 1 ]

Edit: And the advantage of using _ over .reduce() is that it is (slightly) more readable, and the result is in the requested format.

Upvotes: 1

Bexultan Zhadyrassynov
Bexultan Zhadyrassynov

Reputation: 372

You can do it like this:

var orders = [
  {
    name: 'order-1',
    date: '2017-05-30 16:38:30.418Z'
  },
  {
    name: 'order-2',
    date: '2017-05-30 16:38:30.418Z'
  },
  {
    name: 'order-3',
    date: '2017-05-31 16:38:30.418Z'
  }
];

var res = [];

orders.forEach(function (elem) {
    var date = elem.date.split(' ')[0];


    if (res[date]) {
        res[date] += 1;
    } else {
        res[date] = 1;
    }
});

console.log(res); // [2017-05-30: 2, 2017-05-31: 1]

Upvotes: 1

mhodges
mhodges

Reputation: 11116

You can count the number of occurrences for a given day using the .reduce() function, like so:

The advantage of using .reduce() over _.groupBy().map() is that you only iterate over your data set once, making it (slightly) more efficient.

var orders = [
  {
    name: 'order-1',
    date: '2017-05-30 16:38:30.418Z'
  },
  {
    name: 'order-2',
    date: '2017-05-30 16:38:30.418Z'
  },
  {
    name: 'order-3',
    date: '2017-05-31 16:38:30.418Z'
  }
];

dayCounts = orders.reduce(function (result, order) {
    var day = moment(order.date).format("YYYY-MM-DD");
    if (!result[day]) {
        result[day] = 0;
    }
    result[day]++;
    return result;
}, {});

console.log(dayCounts);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>

Upvotes: 2

Related Questions