Home > Enterprise >  return two sets of of data from two seperate arrays without using nested for loops
return two sets of of data from two seperate arrays without using nested for loops

Time:03-04

Say I have an object like so

const containers = {
  ['Container-1']: {
     ['Object-1']: {
        date: 2022-01-02T10:00:00
     },
     ['Object-2']: {
        date: 2022-02-02T10:00:00
     },
     ['Object-3']: {
        date: 2022-02-02T09:00:00
     }
  },
  ['Container-2']: {
     ['Object-1']: {
        date: 2022-05-02T10:00:00
     },
     ['Object-2']: {
        date: 2022-06-02T10:00:00
     },
     ['Object-3']: {
        date: 2022-07-02T09:00:00
     }
  }
}

and I have a function that takes a container and returns the closest date like so

getLatestDate(containerId) {
   const objects = containers[containerId];
   const orderedObjects = orderBy(Object.entries(objects), (object) => object[1].date, ['desc'])
   const latest = orderedObjects.slice(0, 1);
   // ... there is some other logic that includes a forLoop in here but leaving out for brevity
   return latest;
}

is there a way I can make the containerId argument an array and change the getClosestDate function to return the latest date object for each container that is passed in without using a nested loop?

Obviously I could do this

getLatestDate(containerIds) {
   const containerValues = {}
   containerIds.forEach((container) => {
      const objects = containers[containerId];
      const orderedObjects = orderBy(Object.entries(objects), (object) => object[1].date, ['desc'])
      const latest = orderedObjects.slice(0, 1);
      // ... there is some other logic that includes a forLoop in here but leaving out for brevity
      containerValues[container] = latest;
   });

   return containerValues;
}

so my desired result would be

getLatestDate(['Container-1', 'Container-2']);
// {
//    ['Container-1']: {...} // Object-2,
//    ['Container-2']: {...} // Object-3
// }

However the time complexity isn't great and would be hard to scale.

I apologise if a similar question has been asked before, I wasn't 100% sure what to search for so please point me in the right direction if it has been asked before

Any help would be appreciated!

Thanks

CodePudding user response:

You could reduce the entries and get the one with the largest date.

const
    getLatestDateObject = object => Object.fromEntries([Object.entries(object).reduce((a, b) => a[1].date > b[1].date ? a : b)]),
    containers = { 'Container-1': { 'Object-1': { date: '2022-01-02T10:00:00' }, 'Object-2': { date: '2022-02-02T10:00:00' }, 'Object-3': { date: '2022-02-02T09:00:00' } }, 'Container-2': { 'Object-1': { date: '2022-05-02T10:00:00' }, 'Object-2': { date: '2022-06-02T10:00:00' }, 'Object-3': { date: '2022-07-02T09:00:00' } } };
    
console.log(getLatestDateObject(containers['Container-1']));
console.log(getLatestDateObject(containers['Container-2']));
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related