get value inside object having array and compare with another array

Viewed 54
array1 = [{id: 1, email: 'test1@test.com', group_ids: ["25"], username: 'test1'},
          {id: 2, email: 'test2@test.com', group_ids: ["22"], username: 'test2'},
          {id: 3, email: 'test3@test.com', group_ids: ["25", "20"], username: 'test3'},
          {id: 4, email: 'test4@test.com', 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 ["test1@test.com", "test2@test.com"] but not "test3@test.com". I would appreciate your help.

2 Answers

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

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: 'test1@test.com', group_ids: ["25"], username: 'test1'},
          {id: 2, email: 'test2@test.com', group_ids: ["22"], username: 'test2'},
          {id: 3, email: 'test3@test.com', group_ids: ["25", "20"], username: 'test3'},
          {id: 4, email: 'test4@test.com', 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) );
// => [ "test1@test.com", "test2@test.com", "test3@test.com" ]

Hope that helped. :)


Related