Sampath Reddy Sampath Reddy - 4 months ago 8
AngularJS Question

How to remove array items in array 2 if the items doesnt match with items of array 1 and concat with array 3

I have two arrays:

project_users = [{
username: "sam", name: "Sampath", email: "xyz"
},{
username: "ram", name: "Ram Mohan", email: "asd"
},{
username: "shyam", name: "Shyam pandey", email: "wer"
}]

admin_users = [{
username: "sam", name: "Sampath", email: "xyz"
},{
username: "pandey", name: "Mangal Pandey", email: "yuy"
},{
username: "ameer", name: "Gajini", email: "tyrt"
},{
username: "shyam", name: "Shyam pandey", email: "wer"
}]


I want to create a new array with name
project_admin_user
who are project users list present under admin users.

Expected result:

project_admin_users = [{
username: "sam", name: "Sampath", email: "xyz"
},{
username: "shyam", name: "Shyam pandey", email: "wer"
}]


How can I achieve this in Angular (preferred) or jQuery?

Answer

Here solution:

project_admins=project_users.filter(function(val){ 

  return admin_users.find( function(val2){ 
    return val.username===val2.username 
  });

});

This is pure js ECMAScript 5 solution, check browsers compatibility- http://caniuse.com/#search=forEach