I have this angularjs array: $scope.fav = []
where items (objects) get added with to it on a function call. an example of these objects are {quote: "Some text", controller: some_controller}
When I add a new object to the array, the array is saved in localstorage. The array works exactly how I want it to except when I try to print it in console.log() i get many [object, Object], which I just assumed is the way it is printed. This isn't the main issue though because the array works how it needs to.
The issue I am having is with trying to find if an object is already in an array. I have tried
if ($scope.fav.indexOf({quote: q, controller: c}) == -1)
and this did not seem to work because every object going in was index -1 even if it was already in the array. I presume this is because it is not reading the object correctly.
Lastly I have resorted to this function:
$scope.containsObject = function(obj, list) {
var i;
for (i = 0; i < list.length; i++) {
if (list[i] === obj) {
return true;
}
}
return false;
}
which checks if the object is in the array. Here is how I call it:
$scope.addToFav = function(q, c) {
$scope.value = $scope.containsObject({quote: q, controller: c}, $scope.fav)
console.log($scope.value);
}
I keep getting a negative value for $scope.value even if the object is in the array. Sorry for the long complicating explanation.
Thank you for your insight, Ben