我想將每個到達日期存盤在我的陣列串列中。有人可以告訴我我該怎么做?但是我的陣列仍然是空的..非常感謝你們..
API 回傳的 Json:
{
"reservations": {
"reservationInfo": [
{
"roomStay": {
"arrivalDate": "11am"
},
"WeatherR": {
"sound": "cloudy"
},
},
{
"roomStay": {
"arrivalDate": "7pm"
},
"WeatherR": {
"sound": "cloudy"
},
}
]
}
}
組件.ts
searchForReservation() {
alert('hello');
this.http.get('/api/searchForReservation')
.subscribe((data) => {
this.ddataIno = data;
this.ddataIno = this.ddataIno.result.reservations.reservationInfo;
console.log('number of value', this.ddataIno.length);
console.log('content', this.ddataIno);
for (let i = 0; i <= this.ddataIno[i].length; i ) {
this.list = this.ddataIno.roomStay.arrivalDate;
}
console.log('store array', this.list)
})
}
uj5u.com熱心網友回復:
searchForReservation() {
alert('hello');
this.http.get('/api/searchForReservation')
.subscribe((data) => {
const reservationInfo = this.data.result.reservations.reservationInfo;
this.list = reservationInfo.map(e => e.roomStay.arrivalDate);
})
}
uj5u.com熱心網友回復:
這是 vanilla JS 中的一個作業示例。你需要對角度做一些小的調整,比如this.list = ...而不是let list = ...
使用 Array#map,您可以從 JSON 物件創建一個新陣列
data.reservations.reservationInfo.map(r => r.roomStay.arrivalDate)
let data = {
"reservations": {
"reservationInfo": [{
"roomStay": {
"arrivalDate": "11am"
},
"WeatherR": {
"sound": "cloudy"
},
},
{
"roomStay": {
"arrivalDate": "7pm"
},
"WeatherR": {
"sound": "cloudy"
},
}
]
}
}
// declare your list as an array at the top
// list: []
// below would start off as 'this.list'
let list = data.reservations.reservationInfo.map(r => r.roomStay.arrivalDate);
console.log(list);
uj5u.com熱心網友回復:
您的 for 回圈只是重新分配的值this.list
我建議閱讀陣列方法
我會使用地圖方法,例如
this.list = this.ddataIno.result.reservations.reservationInfo.map(i => i.roomStay.arrivaldate);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/431111.html
上一篇:Python缺少self引數
