[英]Sort outer object based on inner array in Javascript
I need to render a cards based on a array of objects from the API response, which look like this (simplified):我需要根据来自 API 响应的对象数组渲染卡片,如下所示(简化):
[
{
name: 'John',
vouchers: [
{
voucherId: 1,
issuedAt: '2020-12-15T16:26:36.3548091+01:00'
},
{
voucherId: 2,
issuedAt: '2020-12-15T16:26:36.3548091+01:00'
},
]
},
{
name: 'Lisa',
vouchers: [
{
voucherId: 1,
issuedAt: '2020-12-13T16:26:36.3548091+01:00'
}
]
}]
Now I want to NgFor over this array of objects (with in it an array of objects) and sort (the outer object) based on the 'issuedAt' date.现在我想对这个对象数组(其中包含一个对象数组)进行 NgFor 并根据“issuedAt”日期对(外部对象)进行排序。
note: the issuedAt date for the array of vouchers is always the same for each user.注意:对于每个用户,优惠券数组的发布日期始终相同。
I was thinking of some combination of using.map =>.sort => Number(new Date(DATE_HERE)), but couldn't get it to work我在想一些 using.map =>.sort => Number(new Date(DATE_HERE)) 的组合,但无法让它工作
If you want the person with the most-recent time to be listed first, get the max issue date for all vouchers and subtract them.如果您希望首先列出时间最近的人,请获取所有凭证的最长发行日期并减去它们。
Note: I changed the hours so that sorting actually works.注意:我更改了时间以便排序确实有效。
const result = [{ name: 'John', vouchers: [{ voucherId: 1, issuedAt: '2020-12-15T16:26:36.3548091+01:00' }, { voucherId: 2, issuedAt: '2020-12-15T17:26:36.3548091+01:00' }, ] }, { name: 'John', vouchers: [{ voucherId: 1, issuedAt: '2020-12-13T18:26:36.3548091+01:00' }] }]; const sorted = result.sort( ({ vouchers: a = [] }, { vouchers: b = [] }) => Math.max(...a.map(({ issuedAt: d = 0 }) => new Date(d))) - Math.max(...b.map(({ issuedAt: d = 0 }) => new Date(d))) ); console.log(sorted);
.as-console-wrapper { top: 0; max-height: 100%;important; }
Given that the array of vouchers all have the same issuedAt
property, you should be able to do this just using the first entry:鉴于凭证数组都具有相同的issuedAt
属性,您应该能够只使用第一个条目来执行此操作:
const data = [{name: 'John', vouchers: [{voucherId: 1, issuedAt: '2020-12-15T16:26:36.3548091+01:00'}, {voucherId: 2, issuedAt: '2020-12-15T16:26:36.3548091+01:00'}]}, {name: 'Lisa', vouchers: [{voucherId: 1, issuedAt: '2020-12-13T16:26:36.3548091+01:00'}]}] const sorter = ({vouchers: [{issuedAt: x}]}, {vouchers: [{issuedAt: y}]}) => x < y? -1: x > y? 1: 0 console.log ([...data].sort (sorter))
We use [...data]
as a cheap shallow clone;我们使用[...data]
作为廉价的浅层克隆; I prefer not to mutate input data.我不喜欢改变输入数据。 But you could just call data.sort (sorter)
if you actually want to mutate your array.但是如果你真的想改变你的数组,你可以调用data.sort (sorter)
。
used Date.parse() to get milliseconds from date string使用 Date.parse() 从日期字符串中获取毫秒
let data = [{ name: 'John', vouchers: [{ voucherId: 1, issuedAt: '2020-12-15T16:26:36.3548091+01:00', }, { voucherId: 2, issuedAt: '2020-12-15T16:26:36.3548091+01:00', }, ], }, { name: 'Lisa', vouchers: [{ voucherId: 1, issuedAt: '2020-12-13T16:26:36.3548091+01:00', }, ], }, ]; data.sort(function(a, b) { return ( Date.parse(a['vouchers'][0]['issuedAt']) - Date.parse(b['vouchers'][0]['issuedAt']) ); }); console.log(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.