0

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!!

3
  • In Javascript arrays have methods like forEach so you don't have to write out the for loop. Also I think it might be easier to create a different data structure for display. Can you share you HTML for displaying the list? Commented Aug 4, 2017 at 16:28
  • Why not order them ascending directly? Commented Aug 4, 2017 at 16:36
  • I might be easier to have a data structure where something like routers= [ { location: 'location1', name:'name1', routers: { '1', '2' } }] for display Commented Aug 4, 2017 at 16:40

1 Answer 1

1

When you initally get your list or routers create the properties you need to sort on.

Something like:

routers.forEach(function(router) {
    router.location: getRouterLocation(router.HostName);
    router.number: getRouterNumber(router.HostName);
    router.name: getRouterName(router.HostName);
});

Then create your functions getRouterLocation, getRouterNumber, etc.. to parse out the values you need.

function getRouterLocation(hostName) {
    return hostName.substring(0, hostName.indexOf('_');
}
Sign up to request clarification or add additional context in comments.

2 Comments

You're a genius!! Thank you. I just created a new array snagged the last part of the HostName and sorted the array on Name. So simple! Why did i go through all that extra effort haha
Great, glad I could help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.