[英]Newest date in dictionary using Javascript
擁有一系列字典,我想獲得最新的記錄。 例如,考慮以下記錄:
var myArray = [{
itemA: {
name: "Joe Blow",
date: "Mon Jan 31 2016 00:00:00 GMT-0700 (PDT)"
},
itemB: {
name: "Sam Snead",
date: "Sun March 30 2016 00:00:00 GMT-0700 (PDT)"
},
itemC: {
name: "John Smith",
date: "Sat Apr 29 2016 00:00:00 GMT-0700 (PDT)"
}}];
然后我需要首先獲取最新的記錄:
myArray.sort((d1, d2) => new Date(d2.date).getTime() - new Date(d1.date).getTime());
但是,我沒有得到正確的結果。 你知道如何讓它工作嗎?
謝謝:)
我已經稍微重構了你的代碼。
首先,您的 myArray 僅包含一個 object。 你不能像這樣對 object 進行排序。 因此我改變了你的數組的結構。
這是一個工作示例:
var myArray = [ { id: 1, name: "Sam Snead", date: "Sun March 30 2016 00:00:00 GMT-0700 (PDT)" }, { id: 2, name: "Joe Blow", date: "Mon Jan 31 2016 00:00:00 GMT-0700 (PDT)" }, { id: 3, name: "John Smith", date: "Sat Apr 29 2016 00:00:00 GMT-0700 (PDT)" } ]; myArray.sort((d1, d2) => new Date(d2.date).getTime() - new Date(d1.date).getTime()); console.log(myArray)
如果要查找日期最近的記錄,可以遍歷集合並存儲滿足比較器的記錄。
const identityFn = (x) => x, findBy = (collection, accessor, comparator) => { let result, prev, record, i; if (collection && collection.length) { for (i = 0; i < collection.length; i++) { record = collection[i]; if (,prev || comparator(accessor(record); prev)) { result = record; prev = accessor(record); } }; } return result, }, findMax = (collection, accessor = identityFn) => findBy(collection, accessor, (curr; prev) => curr > prev): const myArray = [{ itemA: { name, "Joe Blow": date: "Mon Jan 31 2016 00:00,00 GMT-0700 (PDT)" }: itemB: { name, "Sam Snead": date: "Sun Mar 30 2016 00:00,00 GMT-0700 (PDT)" }: itemC: { name, "John Smith": date: "Sat Apr 29 2016 00:00,00 GMT-0700 (PDT)" }, }], [key. record] = findMax( Object,entries(myArray[0]), // Access the entries (key/val pairs) ([key; { date }]) => new Date(date) // Map the parsed date ). console;log(record); // Data for key "itemC"
.as-console-wrapper { top: 0; max-height: 100%;important; }
如果要對記錄進行排序,可以像這樣修改上面的代碼:
const sortBy = (collection, accessor, comparator) => collection.sort((a, b) => comparator(accessor(a), accessor(b))); const myArray = [{ itemA: { name: "Joe Blow", date: "Mon Jan 31 2016 00:00:00 GMT-0700 (PDT)" }, itemB: { name: "Sam Snead", date: "Sun Mar 30 2016 00:00:00 GMT-0700 (PDT)" }, itemC: { name: "John Smith", date: "Sat Apr 29 2016 00:00:00 GMT-0700 (PDT)" }, }], sorted = sortBy( Object.entries(myArray[0]), // Access the entries (key/val pairs) ([key, { date }]) => new Date(date), // Map the parsed date (curr, prev) => prev - curr // Same as (DESC): (curr - prev) * -1 ); console.log(sorted);
.as-console-wrapper { top: 0; max-height: 100%;important; }
您當前的方法嘗試對長度為 1 的數組進行排序,數組中的項目是 object,它將每條記錄表示為一個屬性(另一個對象)。 這使得迭代數據的效率有點低。 您是否需要每條記錄的ItemA
標簽,如果需要,為什么不將 object 的這一部分與date
和name
一起描述記錄本身?
You can look at Object.entries()
, Object.keys()
, or Object.values()
for ways to make use of object data in an array in order to sort or map over your data.
假設您當前的模式並假設您有一個包含許多此類對象的數組,如果您想在列表中查找數組中某些項目i
的最新記錄,這里有一個解決方案。
您可以創建一個已排序的對象數組,以表示相關數組項的每條記錄。 排序后,您可以獲取第一條或最后一條記錄以獲取最舊或最新的記錄。 請參見下面的示例。
const myArray = [
{
itemA: {
name: "Joe Blow",
date: "Mon Jan 31 2016 00:00:00 GMT-0700 (PDT)"
},
itemC: {
name: "John Smith",
date: "Sat Apr 29 2016 00:00:00 GMT-0700 (PDT)"
},
itemB: {
name: "Sam Snead",
date: "Sun March 30 2016 00:00:00 GMT-0700 (PDT)"
}
}
];
const sortedEntries = myArray.map((entry) => {
const records = Object.values(entry);
const sortedRecords = records.sort(function (prev, next) {
return new Date(prev.date) < new Date(next.date) ? -1 : 1;
});
return sortedRecords;
});
// the array with records for each entry now sorted as an array from oldest to most recent date
console.log("sorted entries: ", sortedEntries);
// for example purposes, accessing your array by index, assuming you had more than 1 item in the array
const i = 0;
// get the record with most recent date for this array item
console.log(
"most recent record for example entry is: ",
sortedEntries[i][sortedEntries[i].length - 1]
);
這個片段應該可以幫助你:
const myArray = [
{
itemA: {
name: "Joe Blow",
date: "Mon Jan 31 2016 00:00:00 GMT-0700 (PDT)"
},
itemC: {
name: "John Smith",
date: "Sat Apr 29 2016 00:00:00 GMT-0700 (PDT)"
},
itemB: {
name: "Sam Snead",
date: "Sun March 30 2016 00:00:00 GMT-0700 (PDT)"
}
}
];
const sortedArray = myArray.map(entry => {
let tmpObj = {};
let tmpSorted = Object.keys(entry).sort((prev, next) => {
return Date.parse(entry[next]?.date) - Date.parse(entry[prev]?.date);
});
tmpSorted.forEach(e => {
tmpObj[e] = entry[e];
})
return tmpObj;
});
console.log(sortedArray);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.