Here, I have one main array called mainArray. In this array I added multiple users list. It can be increased. I can create many group from this mainarray, like group1, group2 and so on. My requirement is, i want to filter objects from mainArray, which haven't added in the groups.
Array main,
var mainArray = [{
userId: "M000001",
name: "Jhon",
companyId: "C0000021"
}, {
userId: "M000002",
name: "Leon",
companyId: "C0000022"
}, {
userId: "M000003",
name: "Thomas",
companyId: "C0000023"
}, {
userId: "M000004",
name: "Sean",
companyId: "C0000024"
}, {
userId: "M000005",
name: "Paul",
companyId: "C0000025"
}, {
userId: "M000006",
name: "Roldan",
companyId: "C0000026"
}, {
userId: "M000007",
name: "Mike",
companyId: "C0000027"
}, {
userId: "M000008",
name: "Mia",
companyId: "C0000028"
}];
Groups,
var group1 = [{
userId: "M000004",
name: "Sean",
companyId: "C0000024"
}, {
userId: "M000005",
name: "Paul",
companyId: "C0000025"
}];
var group2 = [{
userId: "M000002",
name: "Leon",
companyId: "C0000022"
}, {
userId: "M000003",
name: "Thomas",
companyId: "C0000023"
}, {
userId: "M000001",
name: "John",
companyId: "C0000021"
}];
Joined group ids,
var joinedGroupIds = ["M000004", "M000005", "M000002", "M000003", "M000001"];
The output which i want to be,
var result = [{
{
userId: "M000006",
name: "Roldan",
companyId: "C0000026"
}, {
userId: "M000007",
name: "Mike",
companyId: "C0000027"
}, {
userId: "M000008",
name: "Mia",
companyId: "C0000028"
}
}];
my javascript code,
var joinGroup, joinedGroupIds = [];
joinGroup = group1.concat(group2);
Concatinated group ids,
joinedGroupIds.map(function(el){
joinedGroupIds.push(el.userId);
});
Filter objects from main array,
var result = mainArray.filter(function(item) {
if (joinedGroupIds.indexOf(item.userId) !== -1) return item;
});