if ( aTimesContacted > bTimesContacted ) {
return 1
}
if ( bTimesContacted > aTimesContacted ) {
return -1
}
return 0;
这是我正在使用的模拟数据集:
mock = [
{
data: {
person: {
contact: {
list: [
{
history: [{...}, {...}, {...}],
type: {}
}
{
history: [{...}],
type: {}
}
]
},
asdf: {...}
},
bleh: {...}
},
something: {...}
},
{...},
{...}
]
我试图按每个父对象中历史记录的总长度过滤模拟数据。
这就是我现在的功能:
function filterByContactAmount():void {
let sorted = data.sort((a:any, b:any) => {
let aTimesContacted:number = a.data.person.contact.list.reduce((acc:any, val:any) => acc + val.history.length, 0)
let bTimesContacted:number = b.data.person.contact.list.reduce((acc:any, val:any) => acc + val.history.length, 0)
return ( aTimesContacted > bTimesContacted ) ? a - b : b - a
})
setData(sorted)
}
我正确地获得了我设置的变量(aTimesContacted
,bTimesContacted
)中的联系时间,但是当我记录排序后的数组时,什么都没有改变。
我在这里想念什么?