2
rated 0 times
[
9]
[
7]
/ answers: 1 / hits: 27409
/ 10 Years ago, wed, april 16, 2014, 12:00:00
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;
};
More From » arrays