为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

眼眸繁星 2021-12-02 16:57:44
我在 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);        }    }
查看完整描述

3 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

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


let index = 1

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


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


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


尤其是每个 App 信息对象上的两个属性ApplicationId和LastUpdateDateTime,因为您使用===来比较它们,所以===也会比较数据类型(数字、字符串...)和数据值,因此请确保它们相同数据类型也是


希望这有帮助


查看完整回答
反对 回复 2021-12-02
?
DIEA

TA贡献1820条经验 获得超2个赞

它看起来确实是正确的,所以也许在这个代码块之外有一些东西是错误的。可能找到错误的一种方法是注销您比较的 hte 属性。


...

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

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

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

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


查看完整回答
反对 回复 2021-12-02
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

我会做类似下面的事情,虽然我没有看到任何错误,只是一些笨拙的代码:


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


查看完整回答
反对 回复 2021-12-02
  • 3 回答
  • 0 关注
  • 149 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信