Giacomo
Giacomo

Reputation: 341

Remove record from array filter by other array

I have a question about array filtering.

Suppose I have two arrays:

var names = ["john", "sarah", "dennis"];
var emails = ["[email protected]", "[email protected]", "[email protected]"];

and now wow can I compare this data and delete it if the name appears in the email??

as a result I should get a third array without [email protected]:

var results = ["[email protected]","[email protected]"];

Thanks for help.

Upvotes: 1

Views: 57

Answers (5)

Xeewi
Xeewi

Reputation: 39

You can try this

var names = ["john", "sarah", "dennis"];
var emails = ["[email protected]", "[email protected]", "[email protected]"];

const result = emails.filter(email => { 
  let nameInMail = true
  
  names.forEach(name => {
    if (!nameInMail) {
      return
    }

    nameInMail = !(email.indexOf(name) >= 0)  
  })
  
  return nameInMail
})

console.log(result)

EDIT: others answer are better than mine haha Use .some() look like the best way

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You can use Array.filter() and Array.find() in combination:

var names = ["john", "sarah", "dennis"];
var emails = ["[email protected]", "[email protected]", "[email protected]"];
var res = emails.filter(email => !names.find(name => email.includes(name)));
console.log(res);

If you are also concern with IE browsers then use simple function declarations and do not use includes() use indexOf() instead:

var names = ["john", "sarah", "dennis"];
var emails = ["[email protected]", "[email protected]", "[email protected]"];
var res = emails.filter(function(email) {
  return !names.find(function(name) {
     return (email.indexOf(name) !== -1);
  });
})
console.log(res);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386680

You could filter emails and check names if an email contains the name.

var names = ["john", "sarah", "dennis"],
    emails = ["[email protected]", "[email protected]", "[email protected]"],
    result = emails.filter(email => !names.some(name => email.includes(name)));

console.log(result);

Another approach by using a regular expression for the check.

var names = ["john", "sarah", "dennis"],
    regexp = new RegExp(names.join('|'), 'i'),
    emails = ["[email protected]", "[email protected]", "[email protected]"],
    result = emails.filter(email => !regexp.test(email));

console.log(result);

Upvotes: 2

Faly
Faly

Reputation: 13356

Use array.prototype.filter, array.prototype.some and string.prototype.includes:

var names = ["john", "sarah", "dennis"];
var emails = ["[email protected]", "[email protected]", "[email protected]"];
var res = emails.filter(email => !names.some(name => email.includes(name)));

console.log(res);

Upvotes: 1

hsz
hsz

Reputation: 152266

You can try with filter:

var names = ["john", "sarah", "dennis"];
var emails = ["[email protected]", "[email protected]", "[email protected]"];

var result = emails.filter(email => !names.find(name => email.includes(name)));

console.log(result);

Upvotes: 1

Related Questions