简体   繁体   中英

Generate json object from array of dates with occurences of each date

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?

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);

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)

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);

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