-1

I have an array of objects for the next three days, so every day it changes. I want to sort out the objects by the dates. Each object has these elements.

dt: 1649278800
dt_txt: "2022-04-06 21:00:00"

I would like to sort the objects depending on the date, and create a new array with the objects which have the same date. So all the "2022-04-06" dates in one array all the "2022-04-07" in an another array etc.

patrick
  • 59
  • 7
  • What do you want to do? Sort by dates? What did you try so far? What is your code? – andy Apr 06 '22 at 19:49
  • I would like to sort the objects depending on the date, and create a new array with the objects which have the same date. So all the "2022-04-06" dates in one array all the "2022-04-07" in an another array etc. – patrick Apr 06 '22 at 19:55

1 Answers1

0

You can pass a comparer to the sort function that will compare the specific dt_txt property of the objects in the array you wish to sort. After then the sorted array will be grouped by the property dt_txt, shortened including only the date, and each group will be held in the map variable by that value as a key and an array of objects belonging to that datetime:

const compare = ((a, b) => new Date(a.dt_txt).getTime() -
                           new Date(b.dt_txt).getTime());

let objectsList = [
  { dt: 1649278800, dt_txt: "2019-04-06 21:00:00" },
  { dt: 6589034985, dt_txt: "2022-04-30 21:00:00" },
  { dt: 9038475923, dt_txt: "2004-12-01 21:00:00" },
  { dt: 5940382093, dt_txt: "2011-08-05 21:00:00" },
];
objectsList.sort( compare );

let map = {};
objectsList.forEach((o, i) => {
  let shortened = o.dt_txt.substring(0, 10);
  if(typeof map[shortened] === 'undefined')
    map[shortened] = [];
  map[shortened].push(o);
});
console.log({ map });
.as-console-wrapper { min-height: 100%!important; top: 0; }

This answer was inspired by this other one: Sort array of objects by string property value

Diego D
  • 6,156
  • 2
  • 17
  • 30
  • Thanks for the answer, but this returns the same array of objects I had originally. – patrick Apr 06 '22 at 20:06
  • yes I'm so sorry I was missing the second part that I added only now and I should better check – Diego D Apr 06 '22 at 20:07
  • I can't understand how this works though, but thanks it works now! :) – patrick Apr 06 '22 at 20:18
  • 1
    The comparator function might be written in a more generic and shorter way like ... `((a, b) => new Date(a.dt_txt).getTime() - new Date(b.dt_txt).getTime())` ... after all it's dates one wants to sort. – Peter Seliger Apr 06 '22 at 20:21
  • 1
    yes you are right. I really copied that part without too much thinking and I ignored it was possible to make it better since the beginning. I'll make the correction – Diego D Apr 06 '22 at 20:25
  • Within a next iteration step one could replace the `forEach` based approach with a [`reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) based one and e.g. come up with the implement of a re-usable reducer function which would compute the mapped group lists as a direct return value without any outer scope variables/references ... `function groupAndCollectSameDayItem(result, item) { ( result[item.dt_txt.slice(0, 10)] ??= [] ).push(item); return result; }` ... `const map = [... objectsList].sort(compare).reduce(groupAndCollectSameDayItem, {});` – Peter Seliger Apr 06 '22 at 20:45
  • @patrick ... _"I can't understand how this works though, but thanks it works now! :)"_ ... This is one of the answers which actually saddens quite some people of the helping crowd cause these people are also driven by sharing and spreading knowledge. It is also the reason why some of the advocating ones (seemingly righteously) insist that the OP firstly comes up with some of its own attempts which do not need to be perfect at all but show a certain effort/willingness. Thus, which steps of the approach, accepted by the OP, are not clear to the OP? There are always some people willing to help. – Peter Seliger Apr 06 '22 at 20:59