用于与数组进行比较并根据值进行更新的代码永远不会返回更新

Fun*_*kel 2 javascript node.js

我在javascript中有一个代码块,该代码块从API(此数据的真实来源)中获取一系列项目,并且当该数组中每个对象的更新日期都在更新时,应该更新dynamo数据库中的数据不符合我所拥有的 一切对我来说似乎都是正确的,但我总是在回信,即使我已经确认存在更新,也不需要更新任何内容。不太确定我在做什么错。

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Vo *_*yen 5

您的代码中存在一个问题,数组索引从零开始,但是循环从1开始

let index = 1
Run Code Online (Sandbox Code Playgroud)

因此,如果第一个应用程序更新,则代码将无法检测到它。根据您的代码,我将索引编辑为零,并尝试创建一些转储数据,并尝试运行您的代码。看起来工作不错

因此,这里唯一的问题是您为每个更新的应用程序两次将数据推送到arrayOfAppsWithUpdates。因此,请再次仔细检查您的API以确保其正确无误。

特别是每个App信息对象上的两个属性ApplicationIdLastUpdateDateTime,因为使用===进行比较,所以===还将比较数据类型(Number,String ...)和数据值,因此请确保它们相同数据类型

希望有帮助