I'm currently working on a dropdown menu that allows multiple selections, utilizing ng-model, ng-options, and ng-change clauses. However, I've encountered an issue where duplicate objects are being pushed into the model. To solve this problem, I attempted creating a second temporary array to filter out duplicates but was unsuccessful in implementing it. Let me elaborate with some code snippets.
Here are examples of objects being added to the select object:
[{
userProfileID: "3f8c553c-3633-4fe9-a007-4346a783450c",
firstName: 'Austin',
lastName: 'Hunter',
email: 'ahunteroutlook.com',
password: 'admin',
companyProfileID: "86660a5b-7f61-4238-889d-1cc3087947b9",
accessLevel: 'admin'
},
{
userProfileID: "bc579485-95a7-4e8d-bdde-52272923576e",
firstName: 'Ashley',
lastName: 'Jeanette',
email: 'ashleygmail.com',
password: 'admin',
companyProfileID: "86660a5b-7f61-4238-889d-1cc3087947b9",
accessLevel: ''
}]
My ng-change function logic goes like this:
$scope.changedDepartment = function(item) {
$scope.selectUser = [];
$scope.users = [];
$scope.tempUsers.length = 0;
if ($scope.department.length > 0) {
if (item[0].DepartmentUsers.length > 0) {
$scope.users.length = 0;
for (var i = 0; i < item.length; i++) {
for (var j = 0; j < item[i].DepartmentUsers.length; j++) {
if (profile[0].userProfileID != item[i].DepartmentUsers[j].userProfileID) {
$scope.tempUsers.push(item[i].DepartmentUsers[j]);
}
}
}
(apologies for the formatting, had to copy-paste from code which messed up the indentation)
In summary, the function detects a selection in the dropdown, retrieves the associated users within that selection, and adds them to $scope.tempUsers. The ultimate goal is to have these users stored in $scope.users. My challenge lies in removing any duplicates from $scope.tempUsers, a task that I am struggling with. Any assistance or advice on how to achieve this would be greatly appreciated.