0

I have 2 arrays (array1 and array2). I want to compare array1 and array2 by using 'id' as the key and want to create 2 resultant arrays out of array2.

  1. First array should contain all the objects which is present in array2 but not in array1.
  2. Second array should contain all the objects which is present in array1 and array2 both, common ones.
const array1 = [{ id: 176770 }, { id: 176771 }, { id: 176820 }];

const array2 = [
    { id: 176770, classfication: "comeone", type: "typeee" },
    { id: 176771, classfication: "comeone1", type: "typeee1" },
    { id: 176820, classfication: "comeone2", type: "typeee2" },
    { id: 176670, classfication: "comeone", type: "typeee" },
    { id: 176761, classfication: "comeone1", type: "typeee1" },
    { id: 176845, classfication: "comeone2", type: "typeee2" },
];

Please help me out to create these 2 arrays.

ikhvjs
  • 5,316
  • 2
  • 13
  • 36
  • 3
    Does this answer your question? [Difference and intersection of two arrays containing objects](https://stackoverflow.com/questions/33356504/difference-and-intersection-of-two-arrays-containing-objects) – pilchard Dec 29 '21 at 12:44
  • 1
    SO is not a coding service. Show what you have tried and where you are stuck. When practical post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your attempt, noting input and expected output. You can also visit the [help center](https://stackoverflow.com/help) or take the [tour](https://stackoverflow.com/tour) to see what and [How to Ask](https://stackoverflow.com/questions/how-to-ask). – pilchard Dec 29 '21 at 12:45

1 Answers1

1

You can loop over the second array and find for an element with same id in first array. If there is no element present in first array, then your first condition is satisfied (Store it in result1). If the element exists in first array, your second condition is satisfied (Store it in result2)

const array1 = [{ "id": 176770 }, { "id": 176771 }, { "id": 176820 }]

const array2 = [{ "id": 176770, "classfication": "comeone", "type": "typeee" }, { "id": 176771, "classfication": "comeone1", "type": "typeee1" }, { "id": 176820, "classfication": "comeone2", "type": "typeee2" }, { "id": 176670, "classfication": "comeone", "type": "typeee" }, { "id": 176761, "classfication": "comeone1", "type": "typeee1" }, { "id": 176845, "classfication": "comeone2", "type": "typeee2" }]

let result1 = []
let result2 = [];

for (let element of array2) {
  let existingInFirst = array1.find(ele => ele.id == element.id);
  if (!existingInFirst) {
    result1.push(element)
  }
  else{
    result2.push(element)
  }
}

console.log(result1)
console.log(result2)
Deepak
  • 2,660
  • 2
  • 8
  • 23
  • Thank you for your response.. 2 condition is not completely satisfying. I want all the object values for 2 condition. As of now, i'm only getting ID's [ { id: 176770 }, { id: 176771 }, { id: 176820 } ] Please check and help me out – Pravindra Singh Dec 29 '21 at 13:04
  • Alright, so as we are looping over the second array, we are good to store the first array directly. I have updated the answer. Please check. – Deepak Dec 29 '21 at 13:09
  • result2 is not giving correct value again. result2 = [ { id: 176770 }, { id: 176771 }, { id: 176820 } ] + result 1. That's how it's working right now. – Pravindra Singh Dec 29 '21 at 13:15
  • 1
    May I know what exactly you need in `result2`? You simply need every element in both the arrays even when there are duplicate `id`? – Deepak Dec 29 '21 at 13:17
  • 1
    You can use `let result2 = array1.concat(array2);` instead of `let result2 = array1;` Hope this helps – Deepak Dec 29 '21 at 13:18
  • It should simply return the elements of array1 from array2 Id will always be unique result 2 should contain the common elements between array1 and array2 .. like result 2 = [{ "id": 176770, "classfication": "comeone", "type": "typeee" },{ "id": 176771, "classfication": "comeone1", "type": "typeee1" },{ "id": 176820, "classfication": "comeone2", "type": "typeee2" }] – Pravindra Singh Dec 29 '21 at 13:24
  • This is a duplicate with extensive discussion, please just flag to close – pilchard Dec 29 '21 at 13:30