I have a list of routers in array called routerList. I want to display the active router next to the backup router in my view. The router object in the array has an attribute called HostName that has the locationID_Router#_RouterName, for instance WA34_R01_ABCABC. Somewhere in the array is another router that is its pair with the name WA34_R02_ABCABC. I want to reorder the array so when I use ng-repeat those two routers and all the other matched pairs will be next to each other. I wrote a function that will tell me if the names are the same minus 1 character so R01 and R02. Here:
function stringCompare(word1, word2){
var differences = 0;
if(word1 != word2){
if(word1.length == word2.length){
for(i=0; i <= word1.length-1; i++){
if(word1.charAt(i) != word2.charAt(i)){
differences +=1;
}
}
}
}
if(differences == 1){
return true;
}else{
return false;
};
};
I then tried to have a loop within a loop to get the pairs and add them to a new array as it found the pairs but I keep getting errors of cannot read HostName part way through the loop.
function findCouples(array){
var MatchedRouters = [];
for(i=0;i<=array.length-1;i++){
for(var i2=0;i2<=array.length-1;i2++){
if(stringCompare(array[i].HostName,array[i2].HostName)){
MatchedRouters.push(array[i]);
MatchedRouters.push(array[i2]);
}
}
}
return MatchedRouters
};
If there is a better way to do this PLEASE let me know! If not any help you could give me for getting it to work like this would be amazing. Thanks!!