[英]pushing an object giving me push not a function
一直在嘗試調試數小時一直在苦苦掙扎,但我無法做到。
ratings = {
"hotel_a": "2.8",
};
///ratings.push ( { hotel_b : 3.2} );
console.log(ratings);
// total number of stars
const starTotal = 5;
for (const rating in ratings) {
const starPercentage = (ratings[rating] / starTotal) * 100;
const starPercentageRounded = `${(Math.round(starPercentage / 10) * 10)}%`;
document.querySelector(`.${rating}.stars - inner`).style.width = starPercentageRounded;
}
它給了我ratings.push不是函數。
///ratings.push ( { hotel_b : 3.2} );
那是行不通的,因為ratings
是對象而不是數組。
您可以做的是創建一個新的鍵和值。
ratings["hotel_b"] =3.2
要么
ratings.hotel_b =3.2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.