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.
2
Answers
I achieved opposite solution based on my question and response of @im2wddrf. Have a loook on the opposite solution.
Here is my proposed solution:
asset1
) to a flat array.asset2
), and convert that to a flat array of arrays (no objects).