I am using the library lodash
in Angular
to group this data:
data =
[
{
hash: "13m49k9pg70b",
fecha_registro: "2021-10-26T20:28:25.350000Z",
id: 7791,
nombre: "aprueba"
},
{
hash: "13m49k9pg70b",
fecha_registro: "2021-10-26T20:28:25.350000Z",
id: 7792,
nombre: "aprueba"
},
{
hash: "1sd345612se",
fecha_registro: "2021-11-08T21:22:25.350000Z",
id: 7793,
nombre: "btest"
},
{
hash: "1sd345612se",
fecha_registro: "2021-11-08T21:22:25.350000Z",
id: 7794,
nombre: "btest"
},
{
hash: "asd1w345611s",
fecha_registro: "2021-08-11T10:22:21.350000Z",
id: 7794,
nombre: "cvtest"
},
]
What I do is group the data by the properties nombre
, hash
, fecha_registro
to do this in a function I do this:
this.confPersoDocenteClas = _.groupBy(data, (item) => {
return [ item['nombre'], item['hash'], item['fecha_registro']];
});
console.log("DATA AGRUPADA", this.confPersoDocenteClas)
Which returns me correctly grouped data
aprueba,13m49k9pg70b,2021-10-26T20:28:25.350000Z: [{...}, {...}]
btest, 1sd345612se, 2021-11-08T21:22:25.350000Z: [{...}, {...}]
cvtest, asd1w345611s, 2021-11-08T10:12:21.350000Z: [{...}, {...}]
The "problem" I have is that it returns the data in alphabetical order of the key nombre
, and what I need is that it returns me ordered by fecha_registro
, from the most recent to the oldest, in this way:
btest, 1sd345612se, 2021-11-08T21:22:25.350000Z: [{...}, {...}]
cvtest, asd1w345611s, 2021-11-08T10:12:21.350000Z: [{...}, {...}]
aprueba,13m49k9pg70b,2021-10-26T20:28:25.350000Z: [{...}, {...}]
To try this I have added a line to the function indicated above in this way:
let datos = _.orderBy(data, ['fecha_registro'], ['desc']); //linea agregada
this.confPersoDocenteClas = _.groupBy(datos, (item) => {
return [ item['nombre'], item['hash'], item['fecha_registro']];
});
This sorts them but doing the grouping again sorts them back in alphabetical order, in this case by key nombre
. How can I make it return ordered by fecha_registro
? I hope you can help me, thanks in advance.
One possible solution is to group first, then sort. The changes would be minimal:
We use
desc
to order from the most recent date to the oldest.In this case, a function would be used as the second parameter of the method
orderBy()
, in order to access the attribute you need.Here I updated the stackblitz with this solution.
When used
_.groupBy()
with an iterator function, the value you return in it dictates the values to group with and the order in which they will appear in the generated 'key', then it will return the new object with the keys sorted alphabetically.To get them sorted by date, the date should be first in the array you return in the iterator:
This will bring you the ones
keys
in another format (2021-10-26T20:28:25.350000Z,aprueba,13m49k9pg70b
) and will also order them alphabetically, so they will come from the oldest to the most recent. If you want to re-format them as you need you could do something like:EDIT:
If you make
console.log(...)
the object you will still see the keys alphabetically, but that doesn't mean they are in the object...It happens that javascript from the beginning considered objects as simple hashes of values that lacked ordering between each key, that is why it
console.log()
will always order the keysstring
alphabetically and the keysnumber
in increasing order.However, since ES6 the iteration order over these has been defined which says:
This affects functions in general that use their internal keys for execution, such as
Object.keys()
,Object.values()
andObject.entries()
so on.That's why the answer is not wrong, but
console.log()
misleading:This shows that parsing is only if you need to have an object with string keys but ordering to use the iteration functions.
Otherwise it will be more useful to use an array like @Jonatan Lavado showed since they do save the ordering in the logs (since they are basically objects with numeric keys
0, 1, etc...
)