I have a nested array of objects which contains some duplicate values:
[
[
{
name: 'name1',
email: 'email1'
},
{
name: 'name2',
email: 'email2'
}
],
[
{
name: 'name1',
email: 'email1'
}
],
[
{
name: 'name1',
email: 'email1'
},
{
name: 'name2',
email: 'email2'
}
]
]
I want to create a new single array from this data containing only the objects that exist in all the nested arrays:
[
{
name: 'name1',
email: 'email1'
}
]
I have tried the following code:
const filteredArray = arrays.shift().filter(function (v) {
return arrays.every(function (a) {
return a.indexOf(v) !== -1;
});
});
console.log('filteredArray => ', filteredArray);
and:
const filteredArray = arrays.reduce((p,c) => p.filter(e => c.includes(e)));
console.log('filteredArray => ', filteredArray);
However these both only return an empty array.
Would really appreciate any help. TIA