'How can I filter specific emails from an array?

I'm trying to get from an array of multiple emails string this effect if there is more than one email that ends with the same domain name of the email do not show me that filter for example:

const db = ['[email protected]', '[email protected]'];

const check = ['[email protected]', '[email protected]'];

function findNotSimilar(check, db) {

const arr = check.filter((item) => db.indexOf(item) === -1);
return arr;

}

this returns ['[email protected]', '[email protected]'], but I would like to return just ['[email protected]'] array because the email ending with the same domain name



Solution 1:[1]

Here is the solution. But regarding the domains.. you have to consider domains with 2 parts like '.co.uk' in comparison with simple '.com'.

function Test() {
    const db = ['[email protected]', '[email protected]'];
    const check = ['[email protected]', '[email protected]'];
    const dbFiltered = db.map(x => x.split('@')[1].split('.').slice(0, -1).join('.')).filter((x, y, z) => z.indexOf(x) === y);
    const arr = check.filter((item) => dbFiltered.map(x => item.indexOf(x) === -1).every(x => x === true));
    return arr;
}

Solution 2:[2]

Working Demo :

const db = ['[email protected]', '[email protected]'];
const check = ['[email protected]', '[email protected]'];

// Split the array elements with domain name and creating a new array.
const formatDb = db.map((item) => item.split('@')[1]);

// Filtered out the element with different domain name.
const arr = check.filter((item) => formatDb.indexOf(item.split('@')[1]) === -1);

console.log(arr);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 andys
Solution 2 Rohìt Jíndal