gpt4 book ai didi

javascript - 如何按特定属性对 JSON 进行排序以将其显示在 Angular 数据表中

转载 作者:行者123 更新时间:2023-12-01 00:33:27 25 4
gpt4 key购买 nike

下午好,我有一个查询,我想订购一个在使用 api 后获得的 json,以将其显示在数据表中,问题是我不想在后端订购它,而是在通过之前订购它将其添加到数据表中,以便它直接显示在您指定的属性的数据表中。

这是我订阅并获取数据的代码

我的 JSON

enter image description here

  RenderDataTable() {
this.isLoading=true; // El Spinner se muestra mientras se estan consumiendo los datos de la Api.
this.PrtgService.getAllElements(this.table).subscribe(
(res) => {
this.dataSource = new MatTableDataSource();
this.dataSource.data = res; // Asigando a lo que me estoy subscribiendo para asignarlo a un DataSource
this.dataSource.paginator = this.paginator; // Paginando el DataSource
this.isLoading=false; // Si la carga de datos proveniente de la Api fue existosa, dejo de mostrar el Spinner
// console.log(res); // Imprimo para comporbar que todo Ok y estoy consumiendo lo deseado.
this.LengthEquipos=res.length; // Obtengo la longitud del arrray que me traigo de la API.
// console.log(this.LengthEquipos);
},
(error) => {
console.log('Se produjo un error mientras intentaba recuperar '+this.table + '!' + error);
});
}

正如我的回答所示:

enter image description here

但就我而言,我想获取按 Vendor 属性排序的数据

最佳答案

详细说明我关于在将数组分配给表之前对数组进行排序的评论:更改此行:

this.dataSource.data = res; 

对此:

this.dataSource.data = res.sort((a, b) => a.vendedor.localeCompare(b.vendedor));

关于javascript - 如何按特定属性对 JSON 进行排序以将其显示在 Angular 数据表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58330755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com