Sorry my english is not good, hope everyone understand. I have an array:
const arr=[
{
name:"c",
pay:[{
name:"c",
date: "2020-10-02"
},{
name:"cc1",
date: "2020-10-03"
},{
name:"cc2",
date: "2020-09-28"
}]
},{
name:"a",
pay:[{
name:"aa",
date: "2020-10-05"
},{
name:"aa1",
date: "2020-10-03"
},{
name:"aa2",
date: "2020-10-04"
}]
}, {
name:"b",
pay:[{
name:"bb",
date: "2020-10-10"
},{
name:"bb1",
date: "2020-10-04"
},{
name:"bb2",
date: "2020-10-01"
}]
}
];
Const date= new Date("2020-10-05");
I want to sort the parent element by date field. Provided that the element whose date is closest to the date variable will be first And result:
const arr=[
{
name:"a",
pay:[{
name:"aa",
date: "2020-10-05"
},{
name:"aa1",
date: "2020-10-03"
},{
name:"aa2",
date: "2020-10-04"
}]
},{
name:"b",
pay:[{
name:"bb",
date: "2020-10-10"
},{
name:"bb1",
date: "2020-10-04"
},{
name:"bb2",
date: "2020-10-01"
}]
},
{
name:"c",
pay:[{
name:"c",
date: "2020-10-02"
},{
name:"cc1",
date: "2020-10-03"
},{
name:"cc2",
date: "2020-09-28"
}]
}
]
My idea is to get the absolute value Math.abs(date- field date in aray), then the parent element with the smallest value will come first. But I have yet to deal with the logic of each sub-array. Help me please. Thanks