Home > Enterprise >  Wanna compare 2 different array list, one by user input and another list from mongodb
Wanna compare 2 different array list, one by user input and another list from mongodb

Time:08-28

I have been struggling in comparing two different array lists. I need quick solution. It's really haywire to me.

What my scenario is, I wanna remove selected values, if record is not exist in another document or if exist than skip that record only and remove other records.

I made a list of objectIds input by user, when user checked the checkboxes, I am getting that id and storing in an array and making a list as given below:

"id": ["62ff99e4306e722e7201657a",
"62ffb71d2a809d528067eeca",
"62f7ce478ac4537516bdec04",
"62cb1660b851d8bb6af08aa7",
"62cb1770b851d8bb6af08ad8",]

And comparing the above array with the below document brand field:

[
  {
    brand: [
      new ObjectId("62c95cae5db50418e6281916"),
      new ObjectId("62cb1660b851d8bb6af08aa7"),
      new ObjectId("62cb1770b851d8bb6af08ad8")
    ]
  },
  {
    brand: [
      new ObjectId("62c95cae5db50418e6281916"),
      new ObjectId("62cb1660b851d8bb6af08aa7"),
      new ObjectId("62cb1770b851d8bb6af08ad8")
    ]
  },
  {
    brand: [
      new ObjectId("62c95cae5db50418e6281916"),
      new ObjectId("62cb1660b851d8bb6af08aa7"),
      new ObjectId("62cb1770b851d8bb6af08ad8")
    ]
  }
]

Even if you see the second document which I am getting from mongodb has brand field which has duplicate records.

At the end what output I require is, when I click delete all button it has to delete total 3 records except below records which are matching in both arrays:

["62cb1660b851d8bb6af08aa7", "62cb1770b851d8bb6af08ad8"]

Would really appreciate if anyone solve my this issue.

CodePudding user response:

I achieved opposite solution based on my question and response of @im2wddrf. Have a loook on the opposite solution.

const listCatalog = await Catalog.aggregate([
 { $project: { _id: 0, brand: 1 } }, { $unwind: "$brand" },
]);
const checkCatalog = listCatalog.map(val => {
 return val.brand.toString()
});
const uniqueCatalog = _id.filter(val => {
 return checkCatalog.indexOf(val) === -1
});             

console.log(uniqueCatalog, "uniqueCatalog")

CodePudding user response:

Here is my proposed solution:

  1. convert your first array (let's call it asset1) to a flat array.
  2. Then take your second array (let's call it asset2), and convert that to a flat array of arrays (no objects).
  3. Use reduce to compare the two and keep only the common ID's that can be found.
const asset1 = {
  "id": [
    "62ff99e4306e722e7201657a",
    "62ffb71d2a809d528067eeca",
    "62f7ce478ac4537516bdec04",
    "62cb1660b851d8bb6af08aa7",
    "62cb1770b851d8bb6af08ad8",
  ]
}


const asset2 = [
  {
    brand: [
      new ObjectId("62c95cae5db50418e6281916"),
      new ObjectId("62cb1660b851d8bb6af08aa7"),
      new ObjectId("62cb1770b851d8bb6af08ad8")
    ]
  },
  {
    brand: [
      new ObjectId("62c95cae5db50418e6281916"),
      new ObjectId("62cb1660b851d8bb6af08aa7"),
      new ObjectId("62cb1770b851d8bb6af08ad8")
    ]
  },
  {
    brand: [
      new ObjectId("62c95cae5db50418e6281916"),
      new ObjectId("62cb1660b851d8bb6af08aa7"),
      new ObjectId("62cb1770b851d8bb6af08ad8")
    ]
  }
];



// step 1: I would convert asset1 to a simple, flat array
const mod1 = [ ...asset1.id ]


// step 2: I would convert asset2 to an array of arrays, no objects
const mod2 = asset2.map(i => {
  return [ ...i.brand.map(b => b.toString()) ]
});



// now use reduce to extract only the common id's between mod1 and mod2
const result = mod2.reduce((accum, curr) => {
  return accum.filter(a => curr.includes(a));
}, mod1)


console.log(mod1);
console.log(mod2);
console.log(result);




  • Related