Apologies for my limited English proficiency, I hope everyone can follow along. I am dealing with 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 am looking to arrange the parent elements based on the date field, with the one closest to the date variable coming first. The desired result is:
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 approach involves calculating the absolute difference Math.abs(date - date in array) to determine the order of parent elements. However, I am struggling with handling the logic of each sub-array. Any assistance would be greatly appreciated. Thank you.