Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
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

 Answers
18

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;
}

[#71425] Tuesday, April 15, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
grayson

Total Points: 36
Total Questions: 113
Total Answers: 95

Location: Tonga
Member since Fri, Aug 21, 2020
4 Years ago
grayson questions
;