我试图获得这样的价值结构。
JSON示例:
[
{
"operationalVehicleExtendDetails": [
{
"date_Book": "2018-11-14T00:00:00+07:00",
"bookingVehicleProces": null
},
{
"date_Book": "2018-11-15T00:00:00+07:00",
"bookingVehicleProces": null
},
],
"id": 1,
"brand": {
"id": 1,
"name": "Toyota"
},
"type": "Avanza (sample)",
"chassisNumber": "0101010101",
"machineNumber": "1111111111",
"bpkbNumber": "0000000000",
"licenseNumber": "D 54 MPE",
"color": "Black",
"year": 2013,
"annualTax": "2019-11-14T16:17:00.9047974",
"annual5Tax": "2023-11-14T16:17:00.9050494",
"kirValidityPeriod": "2020-11-14T16:17:00.9045803",
"isActive": true
}
]
这是我的getDataFunction
public AllDasboards: Array<any> = [];
public getDataSem() {
this.listvehicleService.getAllDashboard().subscribe(data => {
this.AllDasboards = [];
for(let key in data){
this.AllDasboards.push({
"brand": data[key].brand.name,
"date_book": data[key].operationalVehicleExtendDetails.book_date
})
for (let index in data.operationalVehicleExtendDetails) {
this.AllDasboards.push({
"book_date": data[key].operationalVehicleExtendDetails[index].book_date,
"bookingvehicleproses": data[key].operationalVehicleExtendDetails[index].bookingVehicleProces
});
}
}
console.log(this.AllDasboards);
})
}
这是我从代码中得到的输出: 输出控制台日志数据: 问题是我无法获得价值operationalVehicleExtendDetails [],并且 我只能从“ operationalVehicleExtendDetails” []中获得价值品牌和另一个品牌,有什么解决方案可以解决我的问题?请帮助:)
答案 0 :(得分:0)
如果我的理解是正确的,这将起作用:
public getDataSem() {
this.listvehicleService.getAllDashboard().subscribe(data => {
this.AllDasboards = [];
for(let key in data){
// Changes ==>
var tempBookDatesArray = []
for (let index in data[key].operationalVehicleExtendDetails) {
tempBookDatesArray.push(data[key].operationalVehicleExtendDetails[index].date_Book);
}
this.AllDasboards.push({
"brand": data[key].brand.name,
"date_book": tempBookDatesArray
})
// <==
}
console.log(this.AllDasboards);
})
}
答案 1 :(得分:0)
for..in
用于遍历对象而不是数组的属性。您可以从here
for..in
的引用
所以您应该使用普通的for
来访问元素
for (let key in data) {
this.AllDasboards.push({
"brand": data[key].brand.name
})
for (let index = 0 index < data.operationalVehicleExtendDetails.length; index++) {
this.AllDasboards.push({
"book_date": data[key].operationalVehicleExtendDetails[index].book_date,
"bookingvehicleproses": data[key].operationalVehicleExtendDetails[index].bookingVehicleProces
});
}
}
答案 2 :(得分:0)
将第二个for循环更改为:
for (let index in data[key].operationalVehicleExtendDetails) {
this.AllDasboards.push({
"book_date": data[key].operationalVehicleExtendDetails[index].date_Book,
"bookingvehicleproses": data[key].operationalVehicleExtendDetails[index].bookingVehicleProces
});
}