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

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

與數(shù)組進(jìn)行比較并根據(jù)值更新的代碼從不返回更新

與數(shù)組進(jìn)行比較并根據(jù)值更新的代碼從不返回更新

眼眸繁星 2021-12-02 16:57:44
我在 javascript 中有一個(gè)代碼塊,它從 API(此數(shù)據(jù)的真實(shí)來(lái)源)中獲取一組項(xiàng)目,并且當(dāng)數(shù)組中每個(gè)對(duì)象的更新日期發(fā)生時(shí),它應(yīng)該更新我的發(fā)電機(jī)數(shù)據(jù)庫(kù)中的數(shù)據(jù)與我所擁有的不符。對(duì)我來(lái)說(shuō)一切都很好,但我總是說(shuō)即使我已經(jīng)驗(yàn)證了更新存在,也不需要更新任何內(nèi)容。不太確定我在這里做錯(cuò)了什么。let count = 0;    for (let appToCompare of arrayOfFormattedApps) {        let hasMatch = false;        for (let index = 1; index < currentCachedListOfApps.length; ++index) {            var cachedApp = currentCachedListOfApps[index];            if (cachedApp.ApplicationId === appToCompare.ApplicationId) {                if (cachedApp.LastUpdateDateTime !== appToCompare.LastUpdateDateTime) {                    arrayOfAppsWithUpdates.push(appToCompare);                    hasMatch = true;                    console.log(cachedApp.AppName + ' is being updated')                    ++count;                    break;                }            }        }        if (hasMatch) {            arrayOfAppsWithUpdates.push(appToCompare);        }    }
查看完整描述

3 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

您的代碼中有一個(gè)問(wèn)題,數(shù)組索引從零開始,但您從 1 開始循環(huán)


let index = 1

所以如果第一個(gè)應(yīng)用程序更新,代碼將無(wú)法檢測(cè)到它。根據(jù)您的代碼,我將索引編輯為零并嘗試創(chuàng)建一些轉(zhuǎn)儲(chǔ)數(shù)據(jù),并嘗試運(yùn)行您的代碼。看起來(lái)運(yùn)作良好


const currentCachedListOfApps = [

    {

        ApplicationId: 1,

        AppName: "App 1",

        LastUpdateDateTime: 4

    },

    {

        ApplicationId: 2,

        AppName: "App 2",

        LastUpdateDateTime: 2

    }

];

const arrayOfFormattedApps = [

    {

        ApplicationId: 1,

        AppName: "App 1",

        LastUpdateDateTime: 1

    },

    {

        ApplicationId: 2,

        AppName: "App 2",

        LastUpdateDateTime: 3

    }

];



const arrayOfAppsWithUpdates = [];

let count = 0;

for (let appToCompare of arrayOfFormattedApps) {


    let hasMatch = false;


    for (let index = 0; index < currentCachedListOfApps.length; ++index) {

        var cachedApp = currentCachedListOfApps[index];


        if (cachedApp.ApplicationId === appToCompare.ApplicationId) {

            if (cachedApp.LastUpdateDateTime !== appToCompare.LastUpdateDateTime) {

                arrayOfAppsWithUpdates.push(appToCompare);

                hasMatch = true;

                console.log(cachedApp.AppName + ' is being updated')

                ++count;

                break;

            }

        }

    }


    if (hasMatch) {

        arrayOfAppsWithUpdates.push(appToCompare);

    }

}

console.log(arrayOfAppsWithUpdates);


所以這里唯一的問(wèn)題是你為每個(gè)更新的應(yīng)用程序?qū)?shù)據(jù)推送到arrayOfAppsWithUpdates兩次。所以請(qǐng)?jiān)俅巫屑?xì)檢查您的 API 以確保它正確。


尤其是每個(gè) App 信息對(duì)象上的兩個(gè)屬性ApplicationId和LastUpdateDateTime,因?yàn)槟褂?==來(lái)比較它們,所以===也會(huì)比較數(shù)據(jù)類型(數(shù)字、字符串...)和數(shù)據(jù)值,因此請(qǐng)確保它們相同數(shù)據(jù)類型也是


希望這有幫助


查看完整回答
反對(duì) 回復(fù) 2021-12-02
?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超2個(gè)贊

它看起來(lái)確實(shí)是正確的,所以也許在這個(gè)代碼塊之外有一些東西是錯(cuò)誤的??赡苷业藉e(cuò)誤的一種方法是注銷您比較的 hte 屬性。


...

    console.log(cachedApp.ApplicationId +" === "+ appToCompare.ApplicationId)

if (cachedApp.ApplicationId === appToCompare.ApplicationId) {

    console.log(cachedApp.LastUpdateDateTime +" !== "+ appToCompare.LastUpdateDateTime)

    if (cachedApp.LastUpdateDateTime !== appToCompare.LastUpdateDateTime) {


查看完整回答
反對(duì) 回復(fù) 2021-12-02
?
慕姐8265434

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊

我會(huì)做類似下面的事情,雖然我沒(méi)有看到任何錯(cuò)誤,只是一些笨拙的代碼:


let appsToUpdate = arrayOfFormattedApps.find((appToCompare, index)=> {

    for(let cachedApp in currentCachedListOfApps) {

        if(cachedApp.ApplicationId === appToCompare.ApplicationId) {

            if (cachedApp.LastUpdateDateTime !== appToCompare.LastUpdateDateTime) {

                return appToCompare

            }

        }

    }

})

// Do work on appsToUpdate


查看完整回答
反對(duì) 回復(fù) 2021-12-02
  • 3 回答
  • 0 關(guān)注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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