进入比较两个数组的对象数组-PUG(Jade)

问题描述 投票:0回答:1

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

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

    [
      {
        "hearts": [
          "5e70c63a94b27b164c9b897f",
          "5e723c75e4bfdf4f58c55e32"
        ],
        "_id": "5e6bb1189978fd5afc98c57a",
        "email": "[email protected]",
        "name": "Catherine",
        "photo": "0121b7fe-b2ae-4e75-979d-7dea1a432855.jpeg",
        "__v": 0
      },
      {
        "hearts": [
          "5e723c75e4bfdf4f58c55e32"
        ],
        "_id": "5e6bc41f5915e3d2980a5174",
        "email": "[email protected]",
        "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,日期...)。

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

javascript arrays loops pug
1个回答
0
投票

如果我的理解正确,您可以执行以下操作。遍历所有用户,当您在author.hearts stop中找到他们的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;
    }
}

0
投票

感谢您的回复,@ haron68,我已经尝试了您的建议,但无法访问store.author.hearts值。我可能需要在这里做一个array.map。

我尝试了以下操作(只是为了检查我是否可以得到任何东西):

- const authorHearts = store.map(count=> count.author.hearts);
- user.forEach(function(el) {
            - const id = el.hearts; // not _id because I need to compare `hearts` from "user" with author.hearts from "store"
            - const result = authorHearts.find(el => el.includes(id)); 
            - if (result == id) {
                pre=h.dump('it works') // equivalent of console.log
            - }  
        - })

pre=h.dump(authorHearts)给我以下数组:

[
  [
    "5e70c63a94b27b164c9b897f",
    "5e723c75e4bfdf4f58c55e32"
  ],
  [
    "5e723c75e4bfdf4f58c55e32"
  ],
  [
    "5e6bcc76c4022eae00e22af6"
  ]
]

[pre=h.dump(id)给我以下数组:

[
  "5e70c63a94b27b164c9b897f",
  "5e723c75e4bfdf4f58c55e32"
]
[
  "5e723c75e4bfdf4f58c55e32"
]
[
  "5e6bcc76c4022eae00e22af6"
]

我想知道是否是因为我试图将数组(authorHearts)数组内的值与多个数组(id)内的值进行比较。

我非常讨厌这个问题://非常感谢

© www.soinside.com 2019 - 2024. All rights reserved.