I have an array of objects similar to the following:
var routeArr = [
{start: 1, end: 2},
{start: 1, end: 3},
{start: 1, end: 4},
{start: 2, end: 1},
{start: 3, end: 1},
{start: 4, end: 1}
];
These objects represent the start and end point of lines and as such, {start: 1, end: 2} and {start: 2, end: 1} represent the same line.
I am trying to remove all duplicate lines from the array and cannot find an efficient or elegant way to do it. I have tried a nested loop but, I've been told that is bad practice (and I'm getting errors with my implementation, and it's just ugly).
for(var i = 0, numRoutes = routeArr.length; i < numRoutes; i++) {
var primaryRoute = routeArr[i];
for(var j = 0; j < numRoutes; j++) {
var secondRoute = routeArr[j];
if(primaryRoute.start === secondRoute.end && primaryRoute.end === secondRoute.start) {
routeArr.splice(j, 1);
continue;
}
}
}
Can anyone offer suggestions?