简体   繁体   中英

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'
        }
      ]
    }
  }
]

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.

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> 

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM