简体   繁体   English

迭代 JavaScript 对象,比较值和返回键

[英]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.我有一个对象idTime ,它具有 id 的键值对和采取行动的时间。 I'm having trouble iterating over it in JavaScript by value, and then retrieving the key from it.我无法在 JavaScript 中按值迭代它,然后从中检索密钥。 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.我需要检索最新的id ,在这种情况下是e0adeb25-9870-4185-a947-ae6f4aae2455 ,所以我需要以这种确切的形式比较时间。 I've read abit about the JavaScript reducer but I'm not sure if this is the way to go.我已经阅读了有关 JavaScript 减速器的一些文章,但我不确定这是否可行。

 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.您想获取最新的项目或 ID。 For that, you can sort the idTime object in descending order then take the first item like the latest one.为此,您可以按降序对idTime对象进行排序,然后将第一项与最新项一样。

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 .为此,您可以使用Object.entries()以便我们可以同时保留keyvalue

 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.您需要根据时间降序对对象的条目进行排序,并获取最新日期的 id。

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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