Currently, I'm utilizing angular.js 1.5 along with angular material on the frontend to populate the DOM with objects retrieved from the Database. Within one of the repeaters, I am facing an issue where I need to determine if an object is already present within another array in the in-scope JavaScript. I've attempted using Array.indexOf()
, Array.forEach
, and a standard for loop to iterate over each element, but unfortunately, it's not functioning the way I intend. It seems like I might be overlooking something simple that is hindering the logic from executing properly. Ideally, I am looking for a method that will return false if the current object exists in the array, and true if it does not.
The scenario is that the user wants to add item(s) ObjA to a list (ObjX.Objs) from another list (Objs)
Here is a snippet of my front-end HTML code :
// Within this snippet, there is an object containing an array of objects
<div ng-controller="myFirstController" ng-init="findOne()">
<md-list-item ng-repeat="ObjA in ObjX.Objs" >
</md-list-item>
<div ng-controller="mySecondcontroller" ng-init="find()">
<md-list-item ng-repeat="ObjA in Objs" ng-if="ObjAExists(ObjA, ObjX.Objs)">
</md-list-item>
</div>
</div>
Controller method :
$scope.ObjAExists = function(ObjA, list){
for(var m = 0; m<list.length; m++){
if(list[m] === ObjA){
return false;
}
else return true;
}
}
I also experimented with matching object Ids, but it did not make a difference
$scope.ObjAExists = function(ObjA, list){
for(var m = 0; m<list.length; m++){
if(list[m]._id == ObjA._id){
return false;
}
else return true;
}
}