【问题标题】:How can I search two object arrays for matching properties and then add a property if a value is true如何在两个对象数组中搜索匹配的属性,然后在值为 true 时添加属性
【发布时间】:2018-03-05 19:03:07
【问题描述】:

我目前让我的路由器将 JSON 数据发布到我用所有当前连接的节点编写的服务器。在该数据中,它显示了节点名称和节点正常运行时间。

然后我将这些连接推送到数据库。在存储之前,我正在检查一些事情。我首先检查自上次发布以来在线的节点是否已离线,然后检查现在连接的节点是否以前从未连接过,最后是否已连接的节点是其先前连接的延续或如果它在帖子之间突然离线,然后又在线。

我当前将路由器分类到它们的类别的代码如下所示(reqRouters 是通过发布请求进入的节点数组,而 dbRouters 是存储在数据库中的所有节点的数组):

function splitRouters(reqRouters, dbRouters) {

    let offlineRouters = [];
    let onlineRouters = [];
    let newRouters = [];
    let allRouters = [];

    for(let dbRouter of dbRouters) {
      if(dbRouter.connected === true && !reqRouters.some(reqRouter => 
      reqRouter.user === dbRouter.user)) {
          offlineRouters.push(dbRouter);
      }
    }       

    for(let reqRouter of reqRouters) {
        dbRouters.some(router => router.user === reqRouter.user) ? 
        onlineRouters.push(reqRouter) : newRouters.push(reqRouter);
    }   

    allRouters.push(offlineRouters);
    allRouters.push(onlineRouters);
    allRouters.push(newRouters);

    return allRouters;
}

这适用于将它们拆分,但如果自上次发布数据以来它已脱机,我想将 newConnection 的属性添加到进入 onlineRouters 数组的对象。我在所有新连接的数据库中存储了一个数组,因此每次建立新连接时,我都会将一个新的 Date() 推送到数组中。

我这样比较它们:dbRouter.allConnections[dbRouter.allConnections.length - 1].getTime() + 10000 < reqRouter.uptime.getTime()

我给了一个额外的 10 秒,以允许由于处理导致的任何时间差异。如果路由器已经离线,那么毫秒数会更大,我会知道节点离线并重新在线。

我遇到的困难是在.user 相等时访问个人dbRouterreqRouter 并修改对象以添加.newConnection 的属性,然后再将其推送到onlineRouters。

我正在使用条件来检查不同函数中的该属性,并根据该条件以不同方式处理存储过程。

对于解决该问题或使我的代码的任何其他部分更高效的任何想法表示赞赏。

谢谢你:)

【问题讨论】:

  • 您是否需要同时修改 dbRouter 和 reqRouter 才能获得新的连接?如果是这种情况下推送 dbRouter 或 reqRouter?那么它们是否等效?
  • 不,我只需要修改一个。我修改了哪一个并不重要,我修改了 reqRouter 对象,因为它是我为代码设置的逻辑中最简单的。

标签: javascript arrays node.js javascript-objects


【解决方案1】:

这是我最终得到的满足我需要的代码。

function splitRouters(reqRouters, dbRouters) {

let offlineRouters = [];
let onlineRouters = [];
let newRouters = [];
let allRouters = [];

//Filters all database routers to only keep the routers that are currently stored in the DB as connected
//Removes all routers that are stored in db as connected but are still connected.
offlineRouters = dbRouters.filter(dbRouter => {
    if(dbRouter.connected === true) {
        return !reqRouters.some(reqRouter => {
            return (dbRouter.user === reqRouter.user);
        });
    }
});

//Returns an array of the routers that have made a previous connection and are currently online
//If the router went offline and this is a new connection, add .newConnection = true to the router.
onlineRouters = reqRouters.filter(reqRouter => {
    return dbRouters.some(dbRouter => {
        if ((dbRouter.user === reqRouter.user && (dbRouter.allConnections[dbRouter.allConnections.length - 1].getTime() + 10000) < reqRouter.connectionEstablished.getTime())) {
            reqRouter.newConnection = true;
        }           
        return dbRouter.user === reqRouter.user;
        });
    }); 

//Returns an array of all the routers sent in via the POST but have no previous records in the DB
newRouters = reqRouters.filter(reqRouter => (!dbRouters.some(router => router.user === reqRouter.user)));


//Push all three arrays to allRouters and return the allRouters array.
allRouters.push(offlineRouters);
allRouters.push(onlineRouters);
allRouters.push(newRouters);    
return allRouters;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多