1

I have 3 arrays: firstname, lastname, email:

var names = firstname.map(a => a.firstname);

    var uniqueNames = [];
    $.each(names, function(i, el){
        if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
    });

    var lastnames = lastname.map(a => a.lastname);


    var uniqueLastNames = [];
    $.each(lastnames, function(i, el){
        if($.inArray(el, uniqueLastNames) === -1) uniqueLastNames.push(el);
    });

    var emails = email.map(a => a.email);

and I'm trying to filter the array of emails by first names (how it will be followed by names):

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

it work when I use example:

var emails = ['[email protected]','[email protected]'];

email: [email protected] is deleted. Good result, but when I use: var emails = email.map(a => a.email); Doesn't work

console.log(emails); is giving result like a array of string: ["[email protected]","[email protected]",...]

Raw data: emails: [email protected], [email protected], [email protected], [email protected], [email protected] firstname: john, dennis, alice lastname: doe

and result mail array: [email protected]. rest to the trash.

Looking forward to the help. Thanks in advance

5
  • 1
    please add the raw data and the wanted result as well. Commented Aug 11, 2018 at 8:26
  • I can't suggest an answer unless you give me the desired format & form of result. Commented Aug 11, 2018 at 8:39
  • im using Laravel Eloquent to get data from Mail Table (columns: firstname, lastname, email) in DB, and loads them into javascript like a names, lastnames, emails (they are like a objects of arrays) so I am getting value with map. In result I have 3 arrays of data like a names = ["john","dennis","nina"], the same with lastnames, and mails. How can I give you better informations? Commented Aug 11, 2018 at 8:54
  • it would be easier, if you add the real raw arrays with objects to the question. to get unique values, you could take a different approach, but until you do not add something, it is not possible to write some code for it. even my answer looks to small problem, which might not address the whole picture. Commented Aug 11, 2018 at 8:59
  • I understand, so i will add more informations because i was trying your code and other solutions but it doesnt work. Gimme a moment Commented Aug 11, 2018 at 9:10

1 Answer 1

2

You could filter by checking with an array of the unwanted names.

var emails = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'],
    firstnames = ['john', 'dennis', 'alice'],
    lastnames = ['doe'],
    names = [...firstnames, ...lastnames],
    filtered = emails.filter(e => !names.some(n => e.includes(n)));
    
console.log(filtered);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.