Stefan Simić
Stefan Simić

Reputation: 31

Comparing two arrays of nested objects and return new array of objects if compared values are not the same in javascript

I have been having trouble trying to figure out how to get this to work. I have two arrays of nested objects arr1 and arr2.

let arr1 =[{
        id: 1,
        rideS: [
        {
          id: 12,
          station: {
            id: 23,
            street: "ABC"
          }
        }
      ]
},
  {
    id: 2,
        rideS: [
        {
          id: 13,
          station: {
            id: 24,
            street: "MMM"
          }
        }
      ]
  }
 ]
 
let arr2 = [
      {
        id: 1,
        so: {
          id: 33,
          sos: [{
            id: 44,
            station: {
              id: 55,
              street: "ABC"
            }
          },
          {
            id: 74,
            station: {
              id: 11,
              street: "DDD"
            }
          }
            
            ]
        }
      },
        {
        id: 2,
        so: {
          id: 34,
          sos: [{
            id: 45,
            station: {
              id: 56,
              street: "RRR"
            }
          },
            {
            id: 51,
            station: {
              id: 66,
              street: "ZZZ"
            }
          }
            ]
        }
      },
    {
        id: 3,
        so: {
          id: 35,
          sos: [{
            id: 46,
            station: {
              id: 57,
              street: "MMM"
            }
          },
            {
            id: 75,
            station: {
              id: 66,
              street: "VVV"
            }
          }
            
            ]
        }
      }
   ]

I need to compare arr2 with arr1 using "street" porperty value of first object array in "sos" and as result i need to return not matched objects

For example if i compare this two arrys as result i expect:

let result = [ 
 {
        id: 2,
        so: {
          id: 34,
          sos: [{
            id: 45,
            station: {
              id: 56,
              street: "RRR"
            }
          },
            {
            id: 51,
            station: {
              id: 66,
              street: "ZZZ"
            }
          }
            ]
        }
      }
]

and this is what i have tried to do

let result = arr2.filter(o1 => o1.so.sos.some(o2 => !arr1.some(a1=> a1.rideS.some(a2 => o2.station.street === a2.station.street))))

and as result i get all objects, but i need somthing like this prob:

 let result = arr2.filter(o1 => o1.so.sos[0].some(o2 => !arr1.some(a1=> a1.rideS.some(a2 => o2.station.street === a2.station.street))))

Upvotes: 0

Views: 165

Answers (2)

Nikola Pavicevic
Nikola Pavicevic

Reputation: 23490

Try to replace some with every:

let arr1 = [
  { id: 1, rideS: [{id: 12, station: {id: 23, street: "ABC",},},],},
  { id: 2, rideS: [{id: 13, station: {id: 24, street: "MMM",},},],},
];

let arr2 = [
  {id: 1, so: {id: 33, sos: [{id: 44,station: {id: 55,street: "ABC",},}, {id: 74, station: {id: 11, street: "DDD",},},],},},
  {id: 2, so: {id: 34,sos: [{id: 45, station: {id: 56, street: "RRR",},},{id: 51, station: {id: 66, street: "ZZZ",},},],},},
  {id: 3, so: {id: 35, sos: [{id: 46, station: {id: 57, street: "MMM",},},{id: 75, station: {id: 66, street: "VVV",},},],},},
];
    
let result = arr2.filter(o1 => o1.so.sos.every(o2 => !arr1.some(a1 => a1.rideS.some(a2 => o2.station.street === a2.station.street))))
console.log(result)

Upvotes: 1

vaira
vaira

Reputation: 2270

First we need to take out all the street address in a dictionary named streetDic and then check in array two if no value inside streetDic is present in its streets.

let arr1 =[{
    id: 1,
    rideS: [
    {
      id: 12,
      station: {
        id: 23,
        street: "ABC"
      }
    }
  ]
},
{
id: 2,
    rideS: [
    {
      id: 13,
      station: {
        id: 24,
        street: "MMM"
      }
    }
  ]
}
]

let arr2 = [
  {
    id: 1,
    so: {
      id: 33,
      sos: [{
        id: 44,
        station: {
          id: 55,
          street: "ABC"
        }
      },
      {
        id: 74,
        station: {
          id: 11,
          street: "DDD"
        }
      }
        
        ]
    }
  },
    {
    id: 2,
    so: {
      id: 34,
      sos: [{
        id: 45,
        station: {
          id: 56,
          street: "RRR"
        }
      },
        {
        id: 51,
        station: {
          id: 66,
          street: "ZZZ"
        }
      }
        ]
    }
  },
{
    id: 3,
    so: {
      id: 35,
      sos: [{
        id: 46,
        station: {
          id: 57,
          street: "MMM"
        }
      },
        {
        id: 75,
        station: {
          id: 66,
          street: "VVV"
        }
      }
        
        ]
    }
  }
]



let streetDic = arr1.reduce((dic, o) => {   o.rideS.forEach(r => dic[r.station.street] = true); return dic; } , {})

let result = arr2.filter(o => !o.so.sos.some(s => streetDic[s.station.street]))

console.log(result);

Upvotes: 1

Related Questions