1


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

1

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, This is what I was looking for.
How can I add limitation for this. For example only output from a specific day to another. for example from "2017-06-22" to "2017-06-26"
1

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

1 Comment

Perfect answer. Thank you
1

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

Comments

1

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]]++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.