如何根据最大键获取对象中的值(Date is an Key)
How to get the value in object based on the largest key (Date is an Key)
我将对象的日期作为键,将值作为位置。我想根据最大日期获取位置。这里的位置是相同的。日期键采用 "MM/DD/YYYY" 格式。
var obj ={};
obj['9/10/2016'] = "India-Chennai";
obj['9/15/2016'] = "Australia-Melborne";
obj['9/20/2016'] = "India-Delhi";
obj['9/25/2016'] = "India-Chennai";
从上面的对象中最大的 Date(Key) 是 "9/25/2016" 值是 "India-Chennai" 如何根据最大的键获取值?
提前致谢
你可能会这样做;
var obj = {},
latest = [];
obj['9/20/2016'] = "India-Delhi";
obj['9/10/2016'] = "Japan-Osaka";
obj['9/25/2016'] = "India-Chennai";
obj['9/15/2016'] = "Australia-Melborne";
latest = obj[Object.keys(obj).sort((a,b) => new Date(b).getTime() - new Date(a).getTime())[0]];
console.log(latest);
您可以使用 Object.keys()
、Array.prototype.map()
、Math.max()
、Date.prototype.getTime()
、Array.prototype.indexOf()
var obj = {};
obj['9/10/2016'] = "India-Chennai";
obj['9/15/2016'] = "Australia-Melborne";
obj['9/20/2016'] = "India-Delhi";
obj['9/25/2016'] = "India-Chennai";
var keys = Object.keys(obj);
var map = keys.map(function(date) {return new Date(date).getTime()});
var key = Math.max.apply(Math, map);
var value = obj[keys[map.indexOf(key)]];
console.log(value);
您可以使用 Object.keys()
获取键列表作为数组,然后 .sort()
该数组:
var obj ={};
obj['9/10/2016'] = "India-Chennai";
obj['9/9/2016'] = "Australia-Melborne";
obj['9/25/2016'] = "India-Chennai";
obj['9/20/2016'] = "India-Delhi";
var parseDate = d => (d = d.match(/(\d{1,2})\/(\d{1,2})\/(\d{4})/)) && new Date(d[3],d[1]-1,d[2]);
var latestKey = Object.keys(obj).sort((a, b) => parseDate(a) - parseDate(b)).pop();
var result = obj[latestKey];
console.log(latestKey, result);
我使用了正则表达式来解析日期,这样代码就可以很容易地适应其他日期格式,例如,如果您需要 d/m/yyyy 而不是 m/d/yyyy。
我将对象的日期作为键,将值作为位置。我想根据最大日期获取位置。这里的位置是相同的。日期键采用 "MM/DD/YYYY" 格式。
var obj ={};
obj['9/10/2016'] = "India-Chennai";
obj['9/15/2016'] = "Australia-Melborne";
obj['9/20/2016'] = "India-Delhi";
obj['9/25/2016'] = "India-Chennai";
从上面的对象中最大的 Date(Key) 是 "9/25/2016" 值是 "India-Chennai" 如何根据最大的键获取值?
提前致谢
你可能会这样做;
var obj = {},
latest = [];
obj['9/20/2016'] = "India-Delhi";
obj['9/10/2016'] = "Japan-Osaka";
obj['9/25/2016'] = "India-Chennai";
obj['9/15/2016'] = "Australia-Melborne";
latest = obj[Object.keys(obj).sort((a,b) => new Date(b).getTime() - new Date(a).getTime())[0]];
console.log(latest);
您可以使用 Object.keys()
、Array.prototype.map()
、Math.max()
、Date.prototype.getTime()
、Array.prototype.indexOf()
var obj = {};
obj['9/10/2016'] = "India-Chennai";
obj['9/15/2016'] = "Australia-Melborne";
obj['9/20/2016'] = "India-Delhi";
obj['9/25/2016'] = "India-Chennai";
var keys = Object.keys(obj);
var map = keys.map(function(date) {return new Date(date).getTime()});
var key = Math.max.apply(Math, map);
var value = obj[keys[map.indexOf(key)]];
console.log(value);
您可以使用 Object.keys()
获取键列表作为数组,然后 .sort()
该数组:
var obj ={};
obj['9/10/2016'] = "India-Chennai";
obj['9/9/2016'] = "Australia-Melborne";
obj['9/25/2016'] = "India-Chennai";
obj['9/20/2016'] = "India-Delhi";
var parseDate = d => (d = d.match(/(\d{1,2})\/(\d{1,2})\/(\d{4})/)) && new Date(d[3],d[1]-1,d[2]);
var latestKey = Object.keys(obj).sort((a, b) => parseDate(a) - parseDate(b)).pop();
var result = obj[latestKey];
console.log(latestKey, result);
我使用了正则表达式来解析日期,这样代码就可以很容易地适应其他日期格式,例如,如果您需要 d/m/yyyy 而不是 m/d/yyyy。