简体   繁体   中英

Iterate Over JavaScript Object, Compare Values and Return Key

I have an object idTime which has key value pairs of ids and the time an action was taken. I'm having trouble iterating over it in JavaScript by value, and then retrieving the key from it. Here's the exact object:

const idTime = {
  "3b2f93b8-72a7-4d85-a366-bb17f830b43c": "2021-07-29 08:51:31+00:00",
  "533e9079-7dd2-4719-b502-69a4ca4b60e6": "2021-06-30 22:14:42+00:00",
  "5df5ba4b-2e87-4bdd-a14e-49db6bb11770": "2021-07-29 16:46:38+00:00",
  "e0adeb25-9870-4185-a947-ae6f4aae2455": "2021-07-29 16:51:05+00:00",
};

I know how to print them out:

  const printIds = () => {
    for (var key in idTime ) {
      if (idMap.hasOwnProperty(key)) {
        console.log(key + " -> " + idTime [key]);
      }
    }
  };

I need to retrieve the latest id , which in this case is e0adeb25-9870-4185-a947-ae6f4aae2455 , so I'll need to compare the times in this exact form. I've read abit about the JavaScript reducer but I'm not sure if this is the way to go.

 const idTime = { "3b2f93b8-72a7-4d85-a366-bb17f830b43c": "2021-07-29 08:51:31+00:00", "533e9079-7dd2-4719-b502-69a4ca4b60e6": "2021-06-30 22:14:42+00:00", "5df5ba4b-2e87-4bdd-a14e-49db6bb11770": "2021-07-29 16:46:38+00:00", "e0adeb25-9870-4185-a947-ae6f4aae2455": "2021-07-29 16:51:05+00:00", }; const maxDate = new Date(Math.max(...Object.keys(idTime).map(x => Date.parse(idTime[x])))); console.log({maxDate}); const maxDateId = Object.keys(idTime).filter(id=>Date.parse(idTime[id])==Date.parse(maxDate))[0]; console.log({maxDateId});

You want to get the item or ID which is the latest. For that, you can sort the idTime object in descending order then take the first item like the latest one.

For performing sorting you have to convert the object into an array. For that, you can use Object.entries() so that we can preserve both key and value .

 const idTime = { "3b2f93b8-72a7-4d85-a366-bb17f830b43c": "2021-07-29 08:51:31+00:00", "533e9079-7dd2-4719-b502-69a4ca4b60e6": "2021-06-30 22:14:42+00:00", "5df5ba4b-2e87-4bdd-a14e-49db6bb11770": "2021-07-29 16:46:38+00:00", "e0adeb25-9870-4185-a947-ae6f4aae2455": "2021-07-29 16:51:05+00:00", }; // This makes an array of array with [key, value] pair const entries = Object.entries(idTime); // Sort the entries array desc order entries.sort((a, b) => { return new Date(b[1]).getTime() - new Date(a[1]).getTime(); }); // The first entry is the latest item after sorting console.log('latest id: ', entries[0][0]);
 .as-console-wrapper{min-height: 100%!important; top: 0}

You will need to sort the object's entries on the basis of time in descending order and get the id of the latest date.

 const idTime = { "3b2f93b8-72a7-4d85-a366-bb17f830b43c": "2021-07-29 08:51:31+00:00", "533e9079-7dd2-4719-b502-69a4ca4b60e6": "2021-06-30 22:14:42+00:00", "5df5ba4b-2e87-4bdd-a14e-49db6bb11770": "2021-07-29 16:46:38+00:00", "e0adeb25-9870-4185-a947-ae6f4aae2455": "2021-07-29 16:51:05+00:00", }; let sortedIdTime = Object.entries(idTime).sort((curr,nxt)=>new Date(nxt[1]) - new Date(curr[1])); let firstEntry = sortedIdTime[0]; let resultantId = firstEntry[0]; console.log(resultantId);

Using array sort method and array destructure ;

 const idTime = { "3b2f93b8-72a7-4d85-a366-bb17f830b43c": "2021-07-29 08:51:31+00:00", "533e9079-7dd2-4719-b502-69a4ca4b60e6": "2021-06-30 22:14:42+00:00", "5df5ba4b-2e87-4bdd-a14e-49db6bb11770": "2021-07-29 16:46:38+00:00", "e0adeb25-9870-4185-a947-ae6f4aae2455": "2021-07-29 16:51:05+00:00", }; const sortedTime = Object .entries(idTime) .sort(([key, value] )=> new Date(value[1]) - new Date(value[0]))[0][0] console.log(sortedTime);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM