简体   繁体   中英

Check objects inside same array and different array

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!

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
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM