1

I have Array<object> which contains n objects of type

{
  name: 'random',
  startDate: '2017-11-10 09:00',
  endDate: '2017-11-23 11:00'
}

What i want is, before rendering the results to filter this array in new Array<object which holds the startDate (and its unique, 2017-11-10 09:00 and 2017-11-10 10:00 are not uniques) and a counter how many times this Date is in the data array. How can i achieve that?

Expressingx
  • 1,480
  • 1
  • 14
  • 39
  • Is it Angular? Do you have Lodash? – Dmitry Nov 07 '17 at 12:16
  • Well, if one carefully reads the OP's question and counterchecks it with the linked answers from marking the Q as "duplicate", on clearly sees that none of the approaches there do work, since the OP asks for a list of counter items each item holding a unique start date from the originally provided date list but also the unique start date's total count from this original list. So please just unmark this Q from "duplicate" unless there will be a new "duplicate" link provided by the *Community* that solves the OP's problem here. – Peter Seliger Nov 08 '17 at 15:46
  • @PeterSeliger I have voted to reopen the question.. I am totally agree with you.. Thanks for the comments – Yosvel Quintero Nov 08 '17 at 15:56

2 Answers2

1

You can first use Array.prototype.reduce() like this:

const dates = [{
    name: 'random',
    startDate: '2017-11-10 09:00',
    endDate: '2017-11-23 11:00'
  },
  {
    name: 'random',
    startDate: '2017-11-10 09:00',
    endDate: '2017-11-23 11:00'
  },
  {
    name: 'random',
    startDate: '2017-11-10 09:00',
    endDate: '2017-11-23 11:00'
  },
  {
    name: 'random',
    startDate: '2017-08-12 09:00',
    endDate: '2017-11-23 11:00'
  }
];

const hash = dates.reduce((a, c) => (a[c.startDate] = ++a[c.startDate] || 1, a), {});
const uniqueDates = Object.keys(hash).map(k => ({ startDate: k, counter: hash[k] }));

console.log(uniqueDates);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
1

var dateList = [{
  name: 'foo',
  startDate: '2017-11-10 09:00',
  endDate: '2017-11-23 11:00'
}, {
  name: 'bar',
  startDate: '2017-11-10 09:01',
  endDate: '2017-11-23 11:00'
}, {
  name: 'baz',
  startDate: '2017-11-10 09:00',
  endDate: '2017-11-24 10:00'
}, {
  name: 'biz',
  startDate: '2017-11-10 09:01',
  endDate: '2017-11-25 09:00'
}, {
  name: 'quick',
  startDate: '2017-11-10 09:00',
  endDate: '2017-11-23 11:00'
}, {
  name: 'brown',
  startDate: '2017-12-10 09:00',
  endDate: '2017-11-23 11:00'
}, {
  name: 'fox',
  startDate: '2017-12-10 10:00',
  endDate: '2017-11-23 11:00'
}];


var startDateCountList = dateList.reduce(function (collector, dateItem) {
  var
    startDate = dateItem.startDate,
    dateCount = collector.registry[startDate];

  if (!dateCount) {
    dateCount = collector.registry[startDate] = {
      startDate : dateItem.startDate,
      count     : 0
    };
    collector.list.push(dateCount);
  }
  dateCount.count += 1;

  return collector;

}, {

  registry: {},
  list:     []

}).list;


console.log('startDateCountList : ', startDateCountList);
.as-console-wrapper { max-height: 100%!important; top: 0; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37