I'm creating a bunch of checkboxes for each unique color. However, I'm running into a bug where I have colors that are repeating.
I generate this array of colors using JSON. I loop through all my products, and if the color does not exist in the $scope.colors variable, then push the color object in.
However, it looks like my comparison of identical color objects is failing. They are identical in value, like el1 = {name: 'Red, id: 1} and el2 = {name: 'Red, id: 1}, but it seems like using Array.indexOf(color) always returns -1.
In my angular controller I have this:
$scope.colors = []; // empty array
$scope.setFilterChoices = function(data) {
for (var i = 0; i < data.length; i++) {
var product = data[i];
for (var j = 0; j < product.products_colors.length; j++) {
var product_color = product.products_colors[j];
if ($scope.colors.indexOf(product_color.color) == -1) { // always pushes the color even if the color is the same
$scope.colors.push(product_color.color);
}
}
}
}
Is there another way to see if an object exists in an array?