這個問題在這里已經有了答案: 從物件陣列中,將屬性的值提取為陣列 25 個答案 2天前關閉。
我正在使用easepicker 構建預訂日歷,并使用mySQL 資料庫中的第三方API 匯入資料。我得到了資料庫中的資料,我可以用 Javascript 訪問它(我是新手)。我正在嘗試從陣列陣列中洗掉除一個以外的所有索引,但無論我如何嘗試執行回圈,我都會得到一個未定義的 .pop(),盡管我確保該物件是一個陣列陣列。
$.ajax({
url : 'get_data.php', // this fetches the booking info from 3rd party => DB
type : 'GET', // type of the HTTP request
success : checkAvail,
});
function checkAvail(response) {
results = response;
var results = jQuery.parseJSON(response);
availables = results.filter(a => a.isAvailable === '1');
console.log(availables); // displays the array of arrays for avail dates
availables.forEach((available) => {
available.pop(2); // **Uncaught TypeError: available.pop is not a function
});
console.table(availables);
}
console.log(availables) 輸出
[
{
"id": "79653836",
"date": "2022-07-09",
"isAvailable": "1",
"price": "2188"
},
{
"id": "79653838",
"date": "2022-07-10",
"isAvailable": "1",
"price": "1750"
},
{
"id": "79653840",
"date": "2022-07-11",
"isAvailable": "1",
"price": "1750"
},
{
"id": "79653842",
"date": "2022-07-12",
"isAvailable": "1",
"price": "1750"
}
]
uj5u.com熱心網友回復:
availables
是物件陣列,而不是陣列陣列。
要從物件中獲取特定屬性,請使用.propname
,因此請使用available.date
獲取日期。
您可以使用map()
回傳一個新陣列,該陣列使用它來獲取屬性。
const availables = [
{
"id": "79653836",
"date": "2022-07-09",
"isAvailable": "1",
"price": "2188"
},
{
"id": "79653838",
"date": "2022-07-10",
"isAvailable": "1",
"price": "1750"
},
{
"id": "79653840",
"date": "2022-07-11",
"isAvailable": "1",
"price": "1750"
},
{
"id": "79653842",
"date": "2022-07-12",
"isAvailable": "1",
"price": "1750"
}
];
const dates = availables.map(a => a.date);
console.log(dates);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/497896.html
標籤:javascript jQuery 数组 前锋