Open In App

How to find Objects that Contain the same Elements from Arrays in Another Object ?

To find objects containing identical elements as specified arrays within another object, iterate through the objects. Identify those whose elements match the provided arrays. This involves comparing the presence of identical elements, ensuring all elements from the arrays exist within the target objects. There are various methods to achieve this which are as follows:

Using some() method

The Array.some() method in JavaScript tests whether at least one element in the array satisfies the provided condition. It returns true if any element passes the test; otherwise, it returns false.

Syntax:

arr.some(callback(element,index,array),thisArg);

Example: Using some method to check for existing items, updating uniqueList and dupList accordingly. The results are then displayed for unique and duplicate items.






let objList = [
  { id: 5, Subject: "English" },
  { id: 4, Subject: "Hindi" },
  { id: 3, Subject: "Science" },
  { id: 2, Subject: "Math" },
  { id: 5, Subject: "English" },
];
 
let uniqueList = [];
let dupList = [];
 
function pushToUniqueList(item) {
  if (!uniqueList.some((i) =>
  i.id === item.id)) uniqueList.push(item);
}
 
function pushToDuplicateList(item) {
  if (!dupList.some((i) =>
  i.id === item.id)) dupList.push(item);
}
 
for (let item of objList) {
  uniqueList.some((i) =>
  i.id === item.id)
    ? pushToDuplicateList(item)
    : pushToUniqueList(item);
}
 
console.log("Duplicate list is", dupList);
console.log("Unique list is", uniqueList);

Output
Duplicate list is [ { id: 5, Subject: 'English' } ]
Unique list is [
  { id: 5, Subject: 'English' },
  { id: 4, Subject: 'Hindi' },
  { id: 3, Subject: 'Science' },
  { id: 2, Subject: 'Math' }
]

Using filter() method

The `Array.filter()` method in JavaScript creates a new array with elements that pass a given condition. It applies a provided function to each element, including those that satisfy the condition, resulting in a filtered array.

Syntax:

array.filter(callback(element, index, arr), thisValue)

Example: Using Array.filter() to identify unique and duplicate objects based on the ‘id’ property. Results are logged for both lists, displaying the unique and duplicate objects.




let objList = [
  { id: 1, language: "Html" },
  { id: 4, language: "Css" },
  { id: 3, language: "Javascript" },
  { id: 6, language: "react" },
  { id: 6, language: "react" },
];
 
let uniqueList = [];
let dupList = [];
 
function pushToUniqueList(item) {
  if (!uniqueList.filter((i) =>
  i.id === item.id).length) uniqueList.push(item);
}
 
function pushToDuplicateList(item) {
  if (!dupList.filter((i) =>
  i.id === item.id).length) dupList.push(item);
}
 
for (let item of objList) {
  uniqueList.filter((i) => i.id === item.id).length
    ? pushToDuplicateList(item)
    : pushToUniqueList(item);
}
 
console.log("Duplicate list is", dupList);
console.log("Unique list is", uniqueList);

Output
Duplicate list is [ { id: 6, language: 'react' } ]
Unique list is [
  { id: 1, language: 'Html' },
  { id: 4, language: 'Css' },
  { id: 3, language: 'Javascript' },
  { id: 6, language: 'react' }
]

Article Tags :