【问题标题】:Loop into Array of Objects comparing two arrays - PUG (Jade)循环进入对象数组比较两个数组 - PUG (Jade)
【发布时间】:2020-03-19 14:41:15
【问题描述】:

我正在尝试在 Javascript 中使用 PUG 模板(如果可能)来比较两个数组,当我在 ID 中找到对应关系时,显示一些特定元素。

// First Array : I iterate over "hearts" object 
// Called in PUG : - const user

    [
      {
        "hearts": [
          "5e70c63a94b27b164c9b897f",
          "5e723c75e4bfdf4f58c55e32"
        ],
        "_id": "5e6bb1189978fd5afc98c57a",
        "email": "catherine@catherine.com",
        "name": "Catherine",
        "photo": "0121b7fe-b2ae-4e75-979d-7dea1a432855.jpeg",
        "__v": 0
      },
      {
        "hearts": [
          "5e723c75e4bfdf4f58c55e32"
        ],
        "_id": "5e6bc41f5915e3d2980a5174",
        "email": "marc@marc.com",
        "name": "Marc",
        "photo": "4caa7bfb-6408-4893-a78b-fa6e8e5b03e7.png",
        "__v": 0
      }
    ]
// Second array : I iterate over "author.hearts" object 
// Called in PUG : - const store

    [{
        "product": {
            "categories": [
                1,
                2
            ]
        },
        "_id": "5e6bcc76c4022eae00e22af6",
        "date": "2222-02-20T21:22:00.000Z",
        "author": {
            "hearts": [
                "5e723c75e4bfdf4f58c55e32",
                "5e70c63a94b27b164c9b897f"
            ],
            "_id": "5e6bb1189978fd5afc98c57a",
            "__v": 0
        },
        "created": "2020-03-13T18:09:58.086Z",
        "id": "5e6bcc76c4022eae00e22af6"
    }]

我想遍历第一个数组,找到第一个 ID(此处为 5e70c63a94b27b164c9b897f),遍历第二个数组并查看此 ID 是否存在于“author.hearts”对象中。如果不是,则继续使用第二个 ID,如果存在,则显示找到该 ID 的对象的所有键(标签、照片、_id、日期...)。

在我的示例中,我的数组中只有一个对象,但稍后我会拥有更多。 非常感谢您的帮助

【问题讨论】:

  • 问之前有没有自己尝试过?
  • 这种逻辑最好远离 pug 模板,并在 express 路由中进行预处理。我也同意 Sean 的观点,即您需要根据社区准则发布解决此问题的尝试。
  • 是的@Sean,我以前尝试过,因为我做这一切主要是为了训练,因为我正在使用 nodejs 学习 PUG。我已经删除了我所做的事情,但我正在尝试类似的事情:each val in hearts each obj in userHearts - if(val ==obj) ..... 和@Graham,是的,我稍后会在我的一个控制器中运行它。但现在,我需要在 PUG 中处理它谢谢

标签: javascript arrays loops pug


【解决方案1】:

如果我理解正确,您可以这样做。循环遍历所有用户,当您在 author.hearts 中找到他们的 id 时 停止 循环并返回找到用户的 _id 的对象。

var resultFound = undefined;
try {
    user.forEach((el) => {
       const id = el._id;
       const result = store.find(el => el.author.hearts.includes(id));   
       if (result) {
          resultFound = result;
          throw resultFound;
       }
    });
} catch (e) {
    if (e !== resultFound) {
       throw e;
    }
}

【讨论】:

  • 感谢您的回复,@haron68,我已经尝试了您的建议,但无法访问 store.author.hearts 值。我可能需要在这里做一个array.map。我尝试了以下方法(只是为了检查我是否能得到任何东西):
  • ` - const authorHearts = store.map(count=> count.author.hearts); - user.forEach(function(el) { - const id = el.hearts; // 不是 _id 因为我需要比较来自“user”的hearts 和来自“store”的 author.hearts - const result = authorHearts.find( el => el.includes(id)); - if (result == id) { pre=h.dump('it works') // 相当于console.log - } - })`
  • pre = h.dump(authorhearts)给我以下阵列:[[“5e70c63a94b27b164c9b89f”,“5e723c75e4bfdf4f58c5e32”],[“5e723c75e4bfdf4f58c55e32”],[“5e6bcc76c4022aeae00e22af6”],pre = h。 dump(id) 给了我以下数组: [ "5e70c63a94b27b164c9b897f", "5e723c75e4bfdf4f58c55e32" ] [ "5e723c75e4bfdf4f58c55e32" ] [ "5e6bcc76c4022eae00e22af6" ] 数组 (id)。
  • 我想知道是不是因为我试图将数组 (authorHearts) 数组中的值与多个数组 (id) 中的值进行比较。我很纠结这个问题:/非常感谢
  • Ps:对不起,我有一个答案格式化但stackoverflow删除了它,迫使我使用无法使用代码格式化的评论部分......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多