2
array1 = [{id: 1, email: '[email protected]', group_ids: ["25"], username: 'test1'},
          {id: 2, email: '[email protected]', group_ids: ["22"], username: 'test2'},
          {id: 3, email: '[email protected]', group_ids: ["25", "20"], username: 'test3'},
          {id: 4, email: '[email protected]', group_ids: ["23"], username: 'test4'}]

array2 = [25, 22];

I want to get list of email from array1 whose having group_ids in array2. I've tried below approach but I guess I'm doing wrong.

var user_groupId = [];
var emails = [];
var obj = [];
for (var i=0; i < array2.length; i++) {
    obj.push(array1.find(o => o.group_ids.find( b => b == array2[i])));        
}

for (var i=0; i< obj.length; i++){
    emails.push(obj[i].email);
}
console.log(emails);

Here I get output Array ["[email protected]", "[email protected]"] but not "[email protected]". I would appreciate your help.

2 Answers 2

3

You should use filter, map and some. Also its not good practice that your ids are strings in once place, they should have the same type:

array1.filter(item => item.group_ids.some(id => array2.includes(+id))).map(item => item.email)

The +id converts the string of the array1 items to an int

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

Comments

3

I think you've over complicated this, and you should keep in mind that you're comparing different data-types (Strings <=> Numbers). There's a number of ways of doing this but whatever approach one takes it's important to express a clear idea about what our code is trying to achieve. See the comments and links below for an explanation of my suggestion...

var array1 = [{id: 1, email: '[email protected]', group_ids: ["25"], username: 'test1'},
          {id: 2, email: '[email protected]', group_ids: ["22"], username: 'test2'},
          {id: 3, email: '[email protected]', group_ids: ["25", "20"], username: 'test3'},
          {id: 4, email: '[email protected]', group_ids: ["23"], username: 'test4'}]
var array2 = [25, 22];

function group_Emails(list_array, id_array) {

    var result = [],
        email;

    /* loop over list_array */
    list_array.forEach(obj => {
        
        /* for each item in list_array (obj)
           convert group_ids to integers and
           store in new list_group_ids array */
        var list_group_ids = obj.group_ids.map(num_str => {
            return parseInt( num_str, 10 )
        });
        
        /* compare id_array to list_group_ids */
        id_array.forEach(num => {
            
            /* does list_group_ids contain num? */
            list_group_ids.includes( num ) && (
                /* if so grab email from obj */
                email = obj.email,
            
                /* add email to result if not present 
                   (avoids duplicate entries) */
                -1 === result.indexOf( email ) && result.push( obj.email )
            );
        })
    });

    return result;
}

console.log( group_Emails(array1, array2) );
// => [ "[email protected]", "[email protected]", "[email protected]" ]

Hope that helped. :)


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.