'How to order an array of object during a forEach()?
So, basically I have this array of objects and I need to order them dependently on the value caricoData
, which is a date. Problem is, the value is a string and I can't change that due to other interactions with the array that need it to be a string, so what I did is I made a forEach()
method to turn each caricoData
of each element into a date (DD/MM/YYY). But then I need it to put it back as a string due to it needing to be so.
So what I thought was "What if I make it so that before returning it into a string, I make it so the array sorts itself in the making?"
Problem is, I don't know how to do it. I've tried with:
filterDateCheck(array){
array.sort((a,b) => (a.caricoData > b.caricoData) ? 1 : ((b.caricoData > a.caricoData) ? -1 : 0))
return array
}
But it doesn't work, returning Invalid Date as a result. So, how can I do it?
TS:
check.forEach(e => {
let formattedDate = Utilities.toDateTimeString(e.caricoData)
let splitDate = formattedDate.split("/")
let dateObject = new Date(+splitDate[2], +splitDate[1] - 1, +splitDate[0]);
// Method goes here? //
e.caricoData = dateObject.toString();
});
Array (check
) structure:
export class ResCheck{
descrizioneIntervento: string;
destinazioneDescrizione: string;
fermoLocalita: string;
caricoData: string;
veicoloTarga: string;
destinazione: string;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|