1 回答

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊
使用 JSON.stringfy 和 JSON.parse,我們可以通過(guò)以下方式保存對(duì)象localStorage
let cities = [];
if (localStorage.getItem("cities") !== null) {
cities = JSON.parse(localStorage.getItem("cities"))
}
console.log("cities = " + cities);
for (const city of cities) {
if (city !== "" && !screen_cities.includes(city)) {
const rnd = Math.random().toString();
const code = await add_new_li(city, rnd);
if (code === 1) {
console.warn("for city " + city + " code is " + code);
document.getElementById(rnd).remove();
}
}
}
// -------- save
cities.push('Shen Yang')
localStorage.setItem('cities', JSON.stringify(cities))
添加回答
舉報(bào)