第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

js localStorage for 循環(huán)包含空字符串

js localStorage for 循環(huán)包含空字符串

慕碼人2483693 2024-01-18 14:40:32
我現(xiàn)在正在創(chuàng)建包含天氣信息的網(wǎng)站。我有一個(gè)特殊的表單(.html 文件),它添加新城市的信息作為無(wú)序列表。localStorage當(dāng)用戶(hù)意外刷新頁(yè)面時(shí),我還用于保存城市。localStorage據(jù)我所知,僅存儲(chǔ)字符串。當(dāng)用戶(hù)添加新城市時(shí),腳本會(huì)更新localStorage。為了刷新頁(yè)面,提供了刷新按鈕。當(dāng)用戶(hù)刷新頁(yè)面時(shí),所有城市都消失了,因此腳本將恢復(fù)所有城市l(wèi)ocalStorage。數(shù)組值在 JS 中[]表示為 String 值""。問(wèn)題是,當(dāng)腳本嘗試恢復(fù)城市時(shí),""值也會(huì)作為城市值提供。代碼:    let cities = [];    if (localStorage.getItem("cities") !== null) {        cities = localStorage.getItem("cities").split(',');    }    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();            }        }    }Array.prototype.forEach不是這樣,但由于異步函數(shù)我無(wú)法使用它add_new_li。請(qǐng)幫我。
查看完整描述

1 回答

?
慕標(biāo)琳琳

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))


查看完整回答
反對(duì) 回復(fù) 2024-01-18
  • 1 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)