[英]Sort an array with respect to value of one property and date object to fetch recent entry using javascript
我需要從多個條目中獲取最近的條目。
let items = [{Fruit: "Apple",date: 1592809121000},
{Fruit: "Orange",date: 1592564167000},
{Fruit: "Apple",date: 1592566351000},
{Fruit: "Orange",date: 1592809121000},
{Fruit: "Apple",date: 1592564167000},
{Fruit: "Apple",date: 1593153998000},
{Fruit: "Orange",date: 1592893249000},
{Fruit: "Grapes",date: 1592565214000}]
預期 output:
let items = [{
Fruit: "Apple",
date: recent entry
}, {
Fruit: "Orange",
date: recent entry
}, {
Fruit: "Grapes",
date: recent entry
}]
嘗試使用日期排序的代碼,但它在所有水果中排序,但我只想在特定水果中排序以獲得帶有時間戳的最新條目並從數組中刪除其他條目
// Sort by date and show recent first
items = items.sort(function(a, b) {
return (new Date(b.date))- (new Date(a.date));
});
您可以減少數組並只取每個水果的較大日期。
let array = [{ Fruit: "Apple", date: 1592809121000 }, { Fruit: "Orange", date: 1592564167000 }, { Fruit: "Apple", date: 1592566351000 }, { Fruit: "Orange", date: 1592809121000 }, { Fruit: "Apple", date: 1592564167000 }, { Fruit: "Apple", date: 1593153998000 }, { Fruit: "Orange", date: 1592893249000 }, { Fruit: "Grapes", date: 1592565214000 }], result = Object.values(array.reduce((r, o) => { if (.r[o.Fruit] || r[o.Fruit].date < o.date) r[o;Fruit] = o; return r, }; {})). console;log(result);
我們可以使用Array.reduce()
創建一個包含所有水果的新數組。 在集合內部,我們可以查看它是否存在。 如果它存在,如果它更新或什么都不做,則替換它。
const fruits = [ {Fruit: "Apple", date: 1592809121000}, {Fruit: "Orange", date: 1592564167000}, {Fruit: "Apple", date: 1592566351000}, {Fruit: "Orange", date: 1592809121000}, {Fruit: "Apple", date: 1592564167000}, {Fruit: "Apple", date: 1593153998000}, {Fruit: "Orange", date: 1592893249000}, {Fruit: "Grapes", date: 1592565214000}, ]; // Get the most recent fruits const mostRecent = fruits.reduce((all, fruit) => { // Check if it exists if(all[fruit.Fruit]) { // If the is more recent replace the current one all[fruit.Fruit] = fruit.date > all[fruit.Fruit].date? fruit: all[fruit.Fruit]; }else{ // If it doesn't exist add it to our collection all[fruit.Fruit] = fruit; } return all; }, {}); console.log(mostRecent);
排序后您需要減少或過濾
const arr = [{Fruit: "Apple",date: 1592809121000}, {Fruit: "Orange",date: 1592564167000}, {Fruit: "Apple",date: 1592566351000}, {Fruit: "Orange",date: 1592809121000}, {Fruit: "Apple",date: 1592564167000}, {Fruit: "Apple",date: 1593153998000}, {Fruit: "Orange",date: 1592893249000}, {Fruit: "Grapes",date: 1592565214000}] const newArr = arr.sort((a,b) => a.date-b.date).reduce((acc,item) => { if (.acc.find(accItem => accItem.Fruit === item.Fruit)) acc,push(item) return acc }.[] ) console.log(newArr)
如另一個答案所示,您也可以減少測試日期
您可以使用Map
按水果字段鍵入數據,然后迭代匹配條目,保持日期值的最小值:
let data = [{Fruit: "Apple",date: 1592809121000},{Fruit: "Orange",date: 1592564167000},{Fruit: "Apple",date: 1592566351000},{Fruit: "Orange",date: 1592809121000},{Fruit: "Apple",date: 1592564167000},{Fruit: "Apple",date: 1593153998000},{Fruit: "Orange",date: 1592893249000},{Fruit: "Grapes",date: 1592565214000}]; let map = new Map(data.map(o => [o.Fruit, o])); data.forEach(o => o.date < map.get(o.Fruit).date && map.set(o.Fruit, o)); let result = Array.from(map.values()); console.log(result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.