Search code examples
javascriptmongodbmongooselodash

lodash Mongoose object id difference


I have two arrays of mongoose Object ids and I want to get the difference of these two sets. I am using lodash. My ObjectId array is as follows.

    let a = [ 59dba788b6068c16ca0b5aa6,
  59dba88bb6068c16ca0b5aa8,
  59dba973b6068c16ca0b5aaa,
  59dba973b6068c16ca0b5aab,
  59dbaa0db6068c16ca0b5aad,
  59dbaa0db6068c16ca0b5aae,
  59dce08e996af20b6a6ceea4,
  59e05048739e3d192b64d3cf,
  59e05048739e3d192b64d3d0 ];

  let b= [ 59dba788b6068c16ca0b5aa6,
   59dba88bb6068c16ca0b5aa8,
   59e05048739e3d192b64d3d0,
   59dba973b6068c16ca0b5aaa ];



  console.log(_.difference(allStudentsArrray, presentStudentsArray)); 

& it prints all the elements in a


Solution

  • Instead of using _.difference use _.differenceWith with _.isEqual argument like as follows

    console.log(_.differenceWith(allStudentsArrray, presentStudentsArray,_.isEqual));   
    

    Hope this works!