let L1 = [
{ id: 1, name: 'Object 1' },
{ id: 1, name: 'Object 2' },
{ id: 1, name: 'Object 3' },
{ id: 2, name: 'Object 1' },
{ id: 2, name: 'Object 2' },
{ id: 3, name: 'Object 1' },
{ id: 3, name: 'Object 3' }
];
// Second array
let L2 = ['Object 1', 'Object 3'];
let idsWithMissingNames = L1.reduce((result, item) => {
if (!L2.includes(item.name)) {
result.push(item.id);
}
return result;
}, []);
console.log(idsWithMissingNames);
//expected result is: 2
I included my code as well but it doesn’t work.
2
Answers
Since it is an exclusion AND check, you need to do some pre processing to get the data into a format that it is easy to loop over and find the matches. So create an object with arrays that holds the name.
You will loop over this object and look for all the keys that match. If they all match you exclude it.
Collect counts and filter ones less than L2’s length: