0

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))))
2
  • are you saying if even one of the street name that is preset in Arr1 is matching Arr2, irrespective of any other property it should be removed? Commented Sep 7, 2021 at 15:26
  • Yes sir, if any street name from arr1 have same name as street property in arr2 (but only comparing with first object of array "sos") sould not be returned in results Commented Sep 7, 2021 at 15:44

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

Comments

1

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)

1 Comment

Don't thank me, it was your code, I just spotted your lapsus :) cheers mate :),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.