检查同一数组和不同数组内的对象

发布于 2025-01-11 19:20:14 字数 1602 浏览 0 评论 0原文

我最近在研究一些js对象(我还在学习中)。我陷入了我的一个项目中,我需要检查(对象)数组内的两个对象之间以及它们与不同数组中的另一个对象之间是否相同。如果发生某些情况,这两个初始对象将必须表现不同:

我会尽力尽可能清楚。我的代码有 2 个对象数组:

let array1 = [{name: "New York", value: "A", price: 43, status: "not checked"},
             {name: "Not Specified", value: "A", price: 333, status: "not checked"},
             {name: "Barry", value: "C", price: 48, status: "not checked"},..etc.];

let array2 = [{name: "John", value: "A", price: 23, status: "not checked"},
             {name: "Jerry", value: "A", price: 67, status: "not checked"},
             {name: "Barry", value: "C", price: 48, status: "not checked"},                 
             {name: "Tom", value: "F", price: 23, status: "not checked"},
             {name: "Barry", value: "C", price: 48, status: "not checked"},...etc.];

如您所见,每个对象的初始状态都是“未检查”。现在,我的部分代码循环一个数组(array2,两个数组中较大的一个),以检查在同一位置“i”处,它们是否具有具有不同属性/值的对象:

 for (let i = 0; i < array2.length; i++){
    if (array1[i].value != array2[i].value){
    ...do something...}.

现在,如您所见,还有一个结果在 array1 和 array2 中有共同点:

{name: "Barry", value: "C", price: 48, status: "not checked"}
  

我想做的是:

  • 留在原始 for 循环中,当我到达“Barry”的“i”位置时,我想检查 array2 中是否至少有 2 个对象 它们之间相同(在本例中为“Barry”)。如果是,则:
  • 检查这两个对象是否与 array1 中的任何对象相同(始终为“Barry”,这次是在 array1 中, 这里 “i”位置可以是随机的)。 如果是,那么:
  • 当循环仍在第一个“Barry”结果的“i”位置循环时,第一个结果将执行某些操作,而另一个相同的结果(始终在 array2 中)将表现不同(所以我正在考虑修改第一个结果的状态为“已检查”,因此后续结果必须验证是否具有不同的状态(“未检查”),需要有不同的行为)。 array1 中的结果没有任何变化。

我对文字墙感到抱歉,但作为一个菜鸟,有时很难描述我想做的事情。感谢您的任何建议!

I'm studying a bit of js objects lately (I'm still learning). I'm stucked on a project of mine where I need to check if 2 objects inside an array (of objects) are identical between them and between them and another object in a different array. These two initial objects will have to behave differently if certain instances occurs:

I'll try to be as clear as possible. My code has 2 arrays of objects:

let array1 = [{name: "New York", value: "A", price: 43, status: "not checked"},
             {name: "Not Specified", value: "A", price: 333, status: "not checked"},
             {name: "Barry", value: "C", price: 48, status: "not checked"},..etc.];

let array2 = [{name: "John", value: "A", price: 23, status: "not checked"},
             {name: "Jerry", value: "A", price: 67, status: "not checked"},
             {name: "Barry", value: "C", price: 48, status: "not checked"},                 
             {name: "Tom", value: "F", price: 23, status: "not checked"},
             {name: "Barry", value: "C", price: 48, status: "not checked"},...etc.];

As you can see initial status of every objects is "not checked". Now, part of my code loops one array (array2, the bigger of the two) to check if, at the same position "i", they have objects inside with different properties/values:

 for (let i = 0; i < array2.length; i++){
    if (array1[i].value != array2[i].value){
    ...do something...}.

Now, as you can see there is also one result in common in array1 and array2:

{name: "Barry", value: "C", price: 48, status: "not checked"}
  

What I would like to do is this:

  • staying inside the original for loop, when I arrive at "i" position of "Barry", I want to check if in array2 there are at least 2 objects
    identical between them (in this case the "Barry" one). If yes then:
  • check if these two are identical to any object in array1 (always "Barry", this time in array1,
    here
    the "i" position can be random).
    If yes then:
  • while the loop is still cycling at "i" position of the first "Barry" result, the first result will do something, while the other identical one (always in array2) will behave differently (so I was thinking to modify the status of the first result to "checked", so the subsequent result, will have to verify that having a different status, "not checked", need to behave differently). Nothing happens to the result in array1.

I'm sorry for the wall of text, but being pretty noob is hard to describe sometimes what I'm trying to do. Thanks for any suggestion!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下的泡沫是彩色的 2025-01-18 19:20:14

您需要为第一个数组中的每个元素调用 find 函数。像这样的事情:

array1.forEach(item => {
  var found = array2.find(item2 => item2.value === item.value)
  if (found) {
    // do something
  }
}

You will need to call a find function for each element in the first array. Something like this:

array1.forEach(item => {
  var found = array2.find(item2 => item2.value === item.value)
  if (found) {
    // do something
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文