2

Hello I have a list of dates, stored in an array; there are multiple entries for the same date. I want to get the occurrences of each date in the for of json object, something like :

Expected output: dates:[{date:11/10/2019,count: 5},{date: 11/11/2019,count:4},{date:11/12/2019, count: 5 }]

My array data looks like:

[
  "11/10/2019, 10:44:16 PM",
  "11/10/2019, 10:48:26 AM",
  "11/10/2019, 10:59:41 PM",
  "11/10/2019, 11:37:53 AM",
  "11/10/2019, 12:19:52 PM",
  "11/11/2019, 12:46:59 PM",
  "11/11/2019, 12:59:08 PM",
  "11/11/2019, 1:32:23 PM",
  "11/11/2019, 3:08:01 AM",
  "11/12/2019, 3:21:39 AM",
  "11/12/2019, 3:26:00 PM",
  "11/12/2019, 3:27:13 PM",
  "11/12/2019, 4:43:24 AM",
  "11/12/2019, 4:49:39 PM"
]

I like to consider only the date in the json.

I tried code:

var cur;
var cnt = 0;
for (var i = 0; i < array_dates.length; i++) {
    cur = current.getDate();
    var d = new Date(array_dates[i]);
    var n = d.getDate();
    if (n != cur) {
        if (cnt > 0) {
            newdata[current] = cnt;
        }
        current = new Date(array_dates[i]);
        cnt = 1;
    } else {
        cnt++;
    }
}
newdata = JSON.stringify(newdata);

But I don't get an object in the expected way. I am missing something but not sure how to fix. How can I achieve the desired result with the array?

6
  • Have you searched for Group by? with little modification you can do it Commented Nov 6, 2019 at 11:35
  • There's no such thing as a "JSON Object" Commented Nov 6, 2019 at 11:37
  • @PrashantPimpale no..how can i perform it on my array as the examples i saw are kind of json arrays and have key values, so is it possible to do it in my example. Commented Nov 6, 2019 at 11:38
  • @Andreas Late by 4 years Commented Nov 6, 2019 at 11:38
  • Is the order of the dates important in the resulting array? Commented Nov 6, 2019 at 11:41

3 Answers 3

1

Another one:

var array = [
    "11/10/2019 10:44:16 PM",
    "11/10/2019 10:44:16 PM",
    "11/10/2019 10:43:06 PM",
    "11/10/2019 10:41:16 PM",
    "11/10/2019 10:40:16 PM",
    "11/11/2019 10:29:16 PM",
    "11/11/2019 10:28:16 PM",
    "11/11/2019 10:20:16 PM",
    "11/11/2019 10:29:16 PM",
    "11/12/2019 10:23:16 PM",
    "11/12/2019 10:20:16 PM",
    "11/12/2019 10:19:16 PM",
    "11/12/2019 10:21:16 PM",
    "11/12/2019 10:18:16 PM"
]

var result = [];

array.reduce(function(respObj, value) {
    value = value.substring(0, 10);
    if (!respObj[value]) {
        respObj[value] = {
            Date: value,
            Count: 1
        };
        result.push(respObj[value])
    } else {
        respObj[value].Count++;
    }
    return respObj;
}, {});

console.log(result)

Sign up to request clarification or add additional context in comments.

1 Comment

Nice use of the reference to an object.
1

You can use String.split to get the date portion of the string (to the left of the comma) and then count each value into an object, then processing the counts to produce an output in the form you desire:

const array_dates = [
  "11/10/2019, 10:44:16 PM",
  "11/10/2019, 10:48:26 AM",
  "11/10/2019, 10:59:41 PM",
  "11/10/2019, 11:37:53 AM",
  "11/10/2019, 12:19:52 PM",
  "11/11/2019, 12:46:59 PM",
  "11/11/2019, 12:59:08 PM",
  "11/11/2019, 1:32:23 PM",
  "11/11/2019, 3:08:01 AM",
  "11/12/2019, 3:21:39 AM",
  "11/12/2019, 3:26:00 PM",
  "11/12/2019, 3:27:13 PM",
  "11/12/2019, 4:43:24 AM",
  "11/12/2019, 4:49:39 PM"
];


let counts = array_dates.reduce((c, v) => {
  let d = v.split(',')[0];
  c[d] = (c[d] || 0) + 1;
  return c;
}, {});
let data = {
  dates: []
};
for (date in counts) {
  data.dates.push({
    date: date,
    count: counts[date]
  });
}
console.log(data);

2 Comments

Nice answer but is the order of the dates guaranteed to stay the same? ref
It should as properties in counts will be created (and then processed) in the same order as the dates are encountered in array_dates. But, I don't have sufficiently in-depth knowledge of how JS works to guarantee that.
0
var counts = [];                                       // the result array
var hash = {};                                         // The hash will map a date to an index in the result array 'counts'

for(var i = 0; i < array_dates.length; i++) {          // for each date in array_dates
  var date = array_dates[i].slice(0, 10);              // get the date part alone (a simple slice will do)

  if(hash.hasOwnProperty(date)) {                      // check if the hash contains an entry for this date
    counts[hash[date]].count++;                        // if so get the object of this date and increment its count
  } else {                                             // otherwise
    hash[date] = counts.push(                          // create a new object for this date
      { date: date, count: 1 }                         // ... initialize its count to 1
    ) - 1;                                             // ... and add the index to 'hash' (push will return the new length so the last index is the new length - 1)
  }
}

Example:

var array_dates = [ "11/10/2019, 10:44:16 PM", "11/10/2019, 10:48:26 AM", "11/10/2019, 10:59:41 PM", "11/10/2019, 11:37:53 AM", "11/10/2019, 12:19:52 PM", "11/11/2019, 12:46:59 PM", "11/11/2019, 12:59:08 PM", "11/11/2019, 1:32:23 PM", "11/11/2019, 3:08:01 AM", "11/12/2019, 3:21:39 AM", "11/12/2019, 3:26:00 PM", "11/12/2019, 3:27:13 PM", "11/12/2019, 4:43:24 AM", "11/12/2019, 4:49:39 PM" ];

var counts = [];
var hash = {};

for(var i = 0; i < array_dates.length; i++) {
  var date = array_dates[i].slice(0, 10);

  if(hash.hasOwnProperty(date)) {
    counts[hash[date]].count++;
  } else {
    hash[date] = counts.push(
      { date: date, count: 1 }
    ) - 1;
  }
}

console.log(counts);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.