0

Hello Everyone!
In this case, I have data object inside array and every data have a Unix Time or a Date, I want to count how many data that have a different day or date, the goal is to take the average value of income per day, per week, month, and also year.

as you can see my code below, I have 9 data and 4 different dates or days, it's mean i have 4 counts in data that have a different date, but how can I get dynamically? And sorry for my English

let MyData = [
  {
    date: 1592918643305,
    name: 'Alex'
  },
  {
    date: 1592921952307,
    name: 'Simon'
  },
  {
    date: 1592927378630,
    name: 'Fory'
  },
  {
    date: 1592987102694,
    name: 'John'
  },
  {
    date: 1592987102694,
    name: 'Erwin'
  },
  {
    date: 1593422810672,
    name: 'Muller'
  },
  {
    date: 1593424811785,
    name: 'Thomas'
  },
  {
    date: 1593478227245,
    name: 'Max'
  },
  {
    date: 1593478293013,
    name: 'Tom'
  }
];

function convertToRegularDate(unix) {
  let regularDate = new Date(unix);
  let year = regularDate.getFullYear();
  let month = regularDate.getMonth();
  let date = regularDate.getDate();
  return 'Date: ' + date + '/' + month + '/' + year;
}

MyData.forEach(function (item) {
  console.log('Name: ' + item.name + ' ' +
  convertToRegularDate(item.date)
  );
});
Firmansyah
  • 106
  • 12
  • 27

3 Answers3

1

You can create an array to store unique dates by pushing the date strings you processed from the function you wrote. Those date strings are going to be how you compare if they are unique. So, after all the data is processed in the for-each then you can just get the length of the array.

let MyData = [
  {
    date: 1592918643305,
    name: 'Alex'
  },
  {
    date: 1592921952307,
    name: 'Simon'
  },
  {
    date: 1592927378630,
    name: 'Fory'
  },
  {
    date: 1592987102694,
    name: 'John'
  },
  {
    date: 1592987102694,
    name: 'Erwin'
  },
  {
    date: 1593422810672,
    name: 'Muller'
  },
  {
    date: 1593424811785,
    name: 'Thomas'
  },
  {
    date: 1593478227245,
    name: 'Max'
  },
  {
    date: 1593478293013,
    name: 'Tom'
  }
];

function convertToRegularDate(unix) {
  let regularDate = new Date(unix);
  let year = regularDate.getFullYear();
  let month = regularDate.getMonth();
  let date = regularDate.getDate();
  return 'Date: ' + date + '/' + month + '/' + year;
}

let uniqueDates = [];
MyData.forEach(function (item) {
  let dateString = convertToRegularDate(item.date);
  if (!uniqueDates.includes(dateString)){
    uniqueDates.push(dateString);
  }
  console.log('Name: ' + item.name + ' ' + dateString);
});
console.log('Unique Date: ' + uniqueDates.length);
holydragon
  • 6,158
  • 6
  • 39
  • 62
  • Thank you for answering, I have my own answer https://stackoverflow.com/questions/840781/get-all-non-unique-values-i-e-duplicate-more-than-one-occurrence-in-an-array, but this is a new method for me, thanks – Firmansyah Jun 30 '20 at 02:40
1

let MyData = [
  {
    date: 1592918643305,
    name: 'Alex'
  },
  {
    date: 1592921952307,
    name: 'Simon'
  },
  {
    date: 1592927378630,
    name: 'Fory'
  },
  {
    date: 1592987102694,
    name: 'John'
  },
  {
    date: 1592987102694,
    name: 'Erwin'
  },
  {
    date: 1593422810672,
    name: 'Muller'
  },
  {
    date: 1593424811785,
    name: 'Thomas'
  },
  {
    date: 1593478227245,
    name: 'Max'
  },
  {
    date: 1593478293013,
    name: 'Tom'
  }
];


function convertToRegularDate(unix) {
  let regularDate = new Date(unix);
  let year = regularDate.getFullYear();
  let month = regularDate.getMonth();
  let date = regularDate.getDate();
  return 'Date: ' + date + '/' + month + '/' + year;
}

MyData.forEach(function (item) {
  console.log('Name: ' + item.name + ' ' +
  convertToRegularDate(item.date)
  );
}); 


const getUniqDates = (data) => [...new Set(data.map(item => new Date(item.date).toLocaleDateString()))]
const uniqDates = getUniqDates(MyData)
console.log("You have " + uniqDates.length + " uniq dates  >> ", ...uniqDates )
Marik Ishtar
  • 2,899
  • 1
  • 13
  • 27
0

You can use Set.

// Set is an object type that has unique values
const set = new Set()

// Iterate through your array
for (let o of MyData) {
    set.add(o.date)
}

// convert to regular date
set.forEach(unixDate => convertToRegularDate(unixDate))

// print the count of data with different date
console.log(set.size)