Home > Enterprise >  Check if date in an array is older than dates in another array
Check if date in an array is older than dates in another array

Time:02-22

I'm trying to check the date from arrayA against the all the dates in arrayB, if the date from arrayA is older than any in arrayB then return.

Not too sure if I am able to check values in an array against other values in an array.

const dateA = arrayA.map((item) => {
  return item.date;

const dateB = arrayB.map((item) => {
  return item.date;

if (dateA >= dateB) {
return 'date is older'}

Dates in arrayA

[ 2022-02-21T05:00:00.000Z ]

Dates in arrayB

[
  2022-02-18T05:00:00.000Z,
  2022-02-10T05:00:00.000Z,
  2022-02-19T05:00:00.000Z
]

Any help would be appreciated!

CodePudding user response:

It's a bit confusing what you are asking, and there are contradictions in your question, so I'll make a few assumptions:

  1. I assume that you actually want to know whether all dates in arrayA are later (newer) or the same - and not older - than all dates in arrayB. (I based this on the fact that you showed >= rather than < in your pseudocode and also showed an example in which this is the case.)

  2. I assume that your arrays contain Date objects. (I based this on the array contents that look like they are copied from an inspect-like output - even though your pseudeocode looks as if there would be objects with a date property inside.)

If something isn't quite right about these assumptions, it should be easy to take the concept anyway and adjust it slightly to your actual needs. If for example you do want to make sure all dates in arrayA are actually older, then you'd just have to swap the two arrays in my examples (or invert all operations). Possibly you might want the condition at the end inverted - again I'm not quite sure what you really want because the question is very ambiguous.


So, for possible solutions: You could of course compare every date in arrayA with every date in arrayB (if (arrayA.every(dateA => arrayB.every(dateB => dateA >= dateB)))), but that would be unnecessarily inefficient because there would have to be arrayA.length * arrayB.length comparisons.

A more efficient option would be to calculate the oldest date in arrayA as well as the newest date in arrayB and then just compare those (which will result in approx. arrayA.length arrayB.length comparisons - note the plus instead of times):

const oldestDateValueA = Math.min(...arrayA.map(Number))
const newestDateValueB = Math.max(...arrayB.map(Number))
if (oldestDateValueA >= newestDateValueB) {
  console.log('Condition fulfilled!')
}

(We could optimize this even further by not using Math.max on the second array but instead .every for comparison with the oldest date of the first array, essentially a mix of the two ways, which would result in fewer comparisons in case a date earlier in the second array would already fail the comparison - however this makes it harder to read and understand in my opinion and the improvement is much less drastic than the previous one we did.)

  • Related