我有一個對象:排序陣列狀物體角
{
"200737212": {
"style": {
"make": {
"id": 200001510,
"name": "Jeep",
"niceName": "jeep"
},
"model": {
"id": "Jeep_Cherokee",
"name": "Cherokee",
"niceName": "cherokee"
},
"price": {
"deliveryCharges": 995,
"baseInvoice": 23360,
"baseMSRP": 23495
},
"id": 200737212,
"name": "Sport 4dr SUV (2.4L 4cyl 9A)",
"trim": "Sport"
},
"config": {
"id": 200737212,
"includedItems": [],
"furtherRemovals": []
},
"lease": {
"leaseStart": 200,
"onePayStart": 150
}
},
"200737213": {
"style": {
"make": {
"id": 200001510,
"name": "Jeep",
"niceName": "jeep"
},
"model": {
"id": "Jeep_Cherokee",
"name": "Cherokee",
"niceName": "cherokee"
},
"price": {
"deliveryCharges": 995,
"baseInvoice": 24083,
"baseMSRP": 24290
},
"id": 200737213,
"name": "Altitude 4dr SUV (2.4L 4cyl 9A)",
"trim": "Altitude"
},
"config": {
"id": 200737213,
"includedItems": [],
"furtherRemovals": []
},
"lease": {
"leaseStart": 300,
"onePayStart": 250
}
},
"200737214": {
"style": {
"make": {
"id": 200001510,
"name": "Jeep",
"niceName": "jeep"
},
"model": {
"id": "Jeep_Cherokee",
"name": "Cherokee",
"niceName": "cherokee"
},
"price": {
"deliveryCharges": 995,
"baseInvoice": 24818,
"baseMSRP": 25295
},
"id": 200737214,
"name": "Latitude 4dr SUV (2.4L 4cyl 9A)",
"trim": "Latitude"
},
"config": {
"id": 200737214,
"includedItems": [],
"furtherRemovals": []
},
"lease": {
"leaseStart": 400,
"onePayStart": 350
}
},
"200737215": {
"style": {
"make": {
"id": 200001510,
"name": "Jeep",
"niceName": "jeep"
},
"model": {
"id": "Jeep_Cherokee",
"name": "Cherokee",
"niceName": "cherokee"
},
"price": {
"deliveryCharges": 995,
"baseInvoice": 28484,
"baseMSRP": 29195
},
"id": 200737215,
"name": "Limited 4dr SUV (2.4L 4cyl 9A)",
"trim": "Limited"
},
"config": {
"id": 200737215,
"includedItems": [],
"furtherRemovals": []
},
"lease": {
"leaseStart": null,
"onePayStart": null
}
}
}
注意
Object.keys(Object) = [200737212, 200737213, 200737214, 200737215]
,它的數據結構如下:
{
{
style: {},
config: {},
lease: {}
},
{
style: {},
config: {},
lease: {}
},
{
style: {},
config: {},
lease: {}
}
}
在對象[ID] .style.price.baseMSRP包含我想要排序的價格價格低 - >高
我創建了一個功能:
function sortByPrice(a: any, b: any) {
console.log(a, b);
const priceA = a.style.price.baseMSRP;
const priceB = b.style.price.baseMSRP;
if (priceA < priceB) {
return -1;
}
if (priceA > priceB) {
return 1;
}
return 0;
}
我試着這樣做:
this.object = this.object.sort(sortByPrice);
但排序沒有內置的對象。
我有一個模板:
<ng-container *ngFor="let id of carIDs">
<md-card *ngIf="activeYear === cars[id]['style'].year.year">
<md-card-content fxFlex>
<ul class="fa-ul">
<li><i class="fa-li fa fa-thermometer-2"></i>{{cars[id]['style'].engine.size}}L {{
cars[id]['style'].engine.cylinder }} Cylinder
</li>
</ul>
</md-card-content>
<md-card-subtitle>Starting MSRP: {{cars[id]['style']?.price.baseMSRP }}
</md-card-subtitle>
<md-card-subtitle *ngIf="cars[id]['lease']?.leaseStart !== null">Starting Monthly Lease: {{
cars[id]['lease']?.leaseStart }}
</md-card-subtitle>
<md-card-subtitle *ngIf="cars[id]['lease']?.onePayStart !== null">Starting One Pay Lease: {{cars[id]['lease']?.onePayStart }}
</md-card-subtitle>
</md-card>
</ng-container>
這使得下面的輸出:
我想模板,通過價格值從object[key].style.price.baseMSRP
在使用ui或管道顯示對象之前,您應該將對象轉換爲數組。 – toskv
@toskv,假設我使用Raulucco方法來轉換數組..沒問題;你可以創建這個管道,以便我可以看到一個例子嗎? (我想開始使用它們) – Moshe
我添加了答案,如果您需要更多關於如何配置它們的信息,我可以添加它們。以防你不使用命令行或者你想要更多的信息。 – toskv