简体   繁体   中英

javascript count and group repeated string in array


I have this array:

["2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-22", "2017-06-22", "2017-06-22", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-25", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-27"]

I'm trying to write a code to get this in output:

['date', "2017-06-2", "2017-06-22", "2017-06-23", "2017-06-24", "2017-06-25"]
['repeated_count', 5, 9, 3, 8, 4]


This is what I did but it's wrong:


var ChartDate = the above data ↑
var ChartDates = [];
var RepeatedNum = [];
for (i =0; i <= ChartDate.length; i++) {
    if (ChartDates.indexOf(ChartDate[i]) < 0) {
        if (typeof ChartDate[i] !== 'undefined') {
            ChartDates.push(ChartDate[i]);
            RepeatedNum.push('T');
        }
    }else {
        RepeatedNum.push(1);
    }
}
console.log(ChartDates);
console.log(RepeatedNum);


How can I solve this issue. Thanks for your help

I gave it a shot and found that it was a little easier to treat the dates/counts as pairs, rather than having them in separate containers. It should be trivial to convert from the output of the below to your desired format.

let output = data.reduce((acc, d)=>{
    e = acc.find(e=> e[0] == d);
    if(e) e[1]++;
    else acc.push([d, 1]);
    return acc;
}, []);

This assumes that your list of dates is called data . output will be a list of 2-element arrays, the first element being the date and the second the count.

Oh, almost forgot: jsfiddle

First, loop through all the dates in the array and tally them into the tally object by either incrementing the key corresponding to the date by 1 if it already exists or making a new key with a value of 1 if it does not. After that, do a for-in loop through the object and populate the uniqueDates array with dates and the repeatedNumber array with the number of repeats.

const dates = ["2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-22", "2017-06-22", "2017-06-22", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-25", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-27"];

const tally = {};
const uniqueDates = ['date'];
const repeatedNumber =['repeated_count'];

for(let i = 0; i < dates.length; i++) {
  if(tally.hasOwnProperty(dates[i])) {
    tally[dates[i]]++;
  } else {
    tally[dates[i]] = 1;
  }
};

for(let date in tally) {
  uniqueDates.push(date);
  repeatedNumber.push(tally[date]);
}

console.log(uniqueDates);
console.log(repeatedNumber);

Looks like this got answered already but here is another option based on the route that you were already headed. I think the reduce version posted is probably the most efficient but if you aren't familiar with reduce then here is another option.

var ChartDate = ["2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-22", "2017-06-22", "2017-06-22", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-25", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-27"]
var ChartDates = ['date'];
var RepeatedNum = ['repeated_count'];
for (i = 0; i < ChartDate.length; i++) {
    if (!ChartDates.includes(ChartDate[i])) {
        ChartDates.push(ChartDate[i]);
        RepeatedNum.push(1);
    } else {
        var index = ChartDates.indexOf(ChartDate[i]);
        RepeatedNum[index] = RepeatedNum[index] + 1;
    }
}
console.log(ChartDates);
console.log(RepeatedNum);

Reusing your code

var chartDate = ["2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-21", "2017-06-22", "2017-06-22", "2017-06-22", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-23", "2017-06-25", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-26", "2017-06-27"];

var chartDates = ['date'];
var repeatedNum = ['repeated_count'];
for (i = 0; i <= chartDate.length; i++) {
  if (chartDates.indexOf(chartDate[i]) === -1) {
    if (typeof chartDate[i] !== 'undefined') {
      chartDates.push(chartDate[i]);
      repeatedNum[chartDates.length - 1] = 1;
    }
  } else {
    var dateIndex = chartDates.indexOf(chartDate[i]);
    repeatedNum[dateIndex]++;
  }
}
console.log(chartDates);
console.log(repeatedNum);

But I think it's better to use an object as opposed to an array, where you can keep the dates as keys and counts as values.

var chartDateMap = {};

for(var i=0; i<chartDate.length; i++) {
    if(!chartDateMap[chartDate[i]]) chartDateMap[chartDate[i]] = 0;
    chartDateMap[chartDate[i]]++;
}

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