繁体   English   中英

根据一个属性的值和日期 object 对数组进行排序,以使用 javascript 获取最近的条目

[英]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.

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