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.