i want to do something like this,
let array1 = [{obj1}, {obj2},{obj3}]
let array2 = [{obj1}, {obj4},{obj5}]
output should be like
{obj1}
This could work for simple objects.
Take in mind that it will not work for functions based properties.
const array1 = [{a:1}, {b:2},{c:3}]
const array2 = [{a:1}, {d:4},{e:5}]
const array1Stringify = array1.map(el => JSON.stringify(el));
const array2Stringify = array2.map(el => JSON.stringify(el));
const result = array1Stringify.filter(el => array2Stringify.includes(el)).map(el => JSON.parse(el));
console.log(result);
As commented, most of your problem is to define how your objects equality will be evaluated. Once you get that resolved, simply checking for the matches of one array in the other shold give you the matches you are after. Most readable and naive way, with a double for.
let array1 = ['hello', 'world', 'I rule']
let array2 = ['hello', 'whatever', 'hey brother']
let matches = [];
for (let i = 0; i < array1.length; i++) {
for (let j = 0; j < array2.length; j++) {
if (array1[i] === array2[j]) { //equality for object problem here
matches.push(array1[i]);
}
}
}
console.log({matches});