与数组进行比较并根据值更新的代码从不返回更新

我在 javascript 中有一个代码块,它从 API(此数据的真实来源)中获取一组项目,并且当数组中每个对象的更新日期发生时,它应该更新我的发电机数据库中的数据与我所拥有的不符。对我来说一切都很好,但我总是说即使我已经验证了更新存在,也不需要更新任何内容。不太确定我在这里做错了什么。


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

        }

    }


眼眸繁星
浏览 150回答 3
3回答

哈士奇WWW

您的代码中有一个问题,数组索引从零开始,但您从 1 开始循环let index = 1所以如果第一个应用程序更新,代码将无法检测到它。根据您的代码,我将索引编辑为零并尝试创建一些转储数据,并尝试运行您的代码。看起来运作良好const currentCachedListOfApps = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ApplicationId: 1,&nbsp; &nbsp; &nbsp; &nbsp; AppName: "App 1",&nbsp; &nbsp; &nbsp; &nbsp; LastUpdateDateTime: 4&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ApplicationId: 2,&nbsp; &nbsp; &nbsp; &nbsp; AppName: "App 2",&nbsp; &nbsp; &nbsp; &nbsp; LastUpdateDateTime: 2&nbsp; &nbsp; }];const arrayOfFormattedApps = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ApplicationId: 1,&nbsp; &nbsp; &nbsp; &nbsp; AppName: "App 1",&nbsp; &nbsp; &nbsp; &nbsp; LastUpdateDateTime: 1&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ApplicationId: 2,&nbsp; &nbsp; &nbsp; &nbsp; AppName: "App 2",&nbsp; &nbsp; &nbsp; &nbsp; LastUpdateDateTime: 3&nbsp; &nbsp; }];const arrayOfAppsWithUpdates = [];let count = 0;for (let appToCompare of arrayOfFormattedApps) {&nbsp; &nbsp; let hasMatch = false;&nbsp; &nbsp; for (let index = 0; index < currentCachedListOfApps.length; ++index) {&nbsp; &nbsp; &nbsp; &nbsp; var cachedApp = currentCachedListOfApps[index];&nbsp; &nbsp; &nbsp; &nbsp; if (cachedApp.ApplicationId === appToCompare.ApplicationId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (cachedApp.LastUpdateDateTime !== appToCompare.LastUpdateDateTime) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arrayOfAppsWithUpdates.push(appToCompare);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasMatch = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(cachedApp.AppName + ' is being updated')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (hasMatch) {&nbsp; &nbsp; &nbsp; &nbsp; arrayOfAppsWithUpdates.push(appToCompare);&nbsp; &nbsp; }}console.log(arrayOfAppsWithUpdates);所以这里唯一的问题是你为每个更新的应用程序将数据推送到arrayOfAppsWithUpdates两次。所以请再次仔细检查您的 API 以确保它正确。尤其是每个 App 信息对象上的两个属性ApplicationId和LastUpdateDateTime,因为您使用===来比较它们,所以===也会比较数据类型(数字、字符串...)和数据值,因此请确保它们相同数据类型也是希望这有帮助

DIEA

它看起来确实是正确的,所以也许在这个代码块之外有一些东西是错误的。可能找到错误的一种方法是注销您比较的 hte 属性。...&nbsp; &nbsp; console.log(cachedApp.ApplicationId +" === "+ appToCompare.ApplicationId)if (cachedApp.ApplicationId === appToCompare.ApplicationId) {&nbsp; &nbsp; console.log(cachedApp.LastUpdateDateTime +" !== "+ appToCompare.LastUpdateDateTime)&nbsp; &nbsp; if (cachedApp.LastUpdateDateTime !== appToCompare.LastUpdateDateTime) {

慕姐8265434

我会做类似下面的事情,虽然我没有看到任何错误,只是一些笨拙的代码:let appsToUpdate = arrayOfFormattedApps.find((appToCompare, index)=> {&nbsp; &nbsp; for(let cachedApp in currentCachedListOfApps) {&nbsp; &nbsp; &nbsp; &nbsp; if(cachedApp.ApplicationId === appToCompare.ApplicationId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (cachedApp.LastUpdateDateTime !== appToCompare.LastUpdateDateTime) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return appToCompare&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }})// Do work on appsToUpdate
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript