How to get the indexOf an object within an AngularJS response object collection?

angularjs, arrays, javascript

Solution

`angular.equals()` does a deep comparison of objects without the `$` prefixed properties...

function arrayObjectIndexOf(arr, obj){
    for(var i = 0; i < arr.length; i++){
        if(angular.equals(arr[i], obj)){
            return i;
        }
    };
    return -1;
}

Problem

Use Case I have a collection of objects returned from a REST request. Angular automatically populates each element with a `$$hashKey`. The problem is that when I search for an object in that array without the `$$hashKey`, it returns -1. This makes sense. Unfortunately, I don't have knowledge of the value of `$$hashKey`. Question Is there a more effective way to search for an object within an object collection returned from a REST request in AngularJS without stripping out the `$$hashKey` property? Code ``` function arrayObjectIndexOf(arr, obj) { var regex = /,?"\$\$hashKey":".*?",?/; var search = JSON.stringify(obj).replace(regex, ''); console.log(search); for ( var i = 0, k = arr.length; i < k; i++ ){ if (JSON.stringify(arr[i]).replace(regex, '') == search) { return i; } }; return -1; }; ```

Original source