Answer the question
In order to leave comments, you need to log in
How to check if an element exists in an array?
Hello!
It is impossible to make through indexOf.
Here is the code
$scope.selectedEditor = function(user)
{
user.originalObject.id = user.originalObject.id * 1;
if($scope.selectedEditors.indexOf(user.originalObject.id) === -1)
{
$scope.selectedEditors.push({
id: user.originalObject.id,
label: user.originalObject.label
});
}
console.log($scope.selectedEditors);
}
<div id="selectedEditors">
<div class="selected_user" ng-repeat="editor in selectedCategory.editors">
<span class="user">{{editor.label}}</span>
<span class="delete_chosen glyphicon glyphicon-remove" ng-click="deleteEditor($index)" title="Удалить"></span>
</div>
</div>
Answer the question
In order to leave comments, you need to log in
user you have is an object that is not a primitive, and indexOf will not work with it.
Options:
1) Keep an array with user ids in parallel and check for the presence of id in it
2) Pass through the array and compare each element with a new one by id.
or think of something else
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question