I have a function that loops an object from a MongoDB collection. It's all possible connections for some mail transportation posts. Once I get one connection, I want to immediately remove the inverse connection from the connections object, for example, postA=1 and postB=2, I want to remove postA=2 and postB=1 (removeConnection
function does that).
I can't understand why it only returns one 'A' on the console when I try to run that function inside calculateRoute
, and returns three 'A' (which is what it should) when I remove it. That function is somehow breaking the loop.
calculatedRoutes = calculateRoute(store.postid, client.postid, connections, []);
function calculateRoute(actualPost, finalPost, connections, routes) {
for(i=0; i < connections.length; i++) {
if(actualPost == connections[i].postA) {
console.log('A');
// If I remove this, the console shows A three times. If I keep this, only shows 1 time.
connections = removeConnection(connections[i].postB, connections[i].postA, connections);
}
}
return routes;
}
function removeConnection(postA, postB, connections) {
for(i=0; i < connections.length; i++) {
if(connections[i].postA == postA && connections[i].postB == postB) {
delete connections[i];
//break;
}
}
return connections;
}