Find the Path between two nodes in JavaScript on custom data structure

Viewed 2635

Hi I have a connections array as below:

var connections =[

    {
      "source": "l1",
      "target": "l2"
    },
    {
      "source": "l2",
      "target": "l4"
    },
    {
      "source": "l2",
      "target": "l3"
    },
    {
      "source": "l4",
      "target": "l5"
    },   

]

It goes on with source and target. Now want to find the path between two nodes using some function. let's say function findConnections("l2", "l5") will return the array like below

var answer =[

        {
          "source": "l2",
          "target": "l4"
        },
        {
          "source": "l4",
          "target": "l5"
        }, 
]

I have no idea how can I achieve this? I tried simple JavaScript but failed. I think using underscore.js or lodash.js we can achieve this? It will be really helpful if anyone provide solution or give hints?

4 Answers
Related