I Have a JSON with countries from a continent. Each country has a array that has it's border countries. Given 2 countries from the same continent, return it's route. E.g.: Brazil and USA -> 1. Colômbia, 2. Panamá, 3. Costa Rica, 4. Nicarágua, 5. Honduras, 6. Guatemala, 7. México.
I'm struggling to organize it on one array only to make a BFS (Breadth First Search). What i have until now is:
function traverse(main_key, obj) {
obj.forEach(function(key) {
if (visited[main_key] === undefined) {
visited[main_key] = new Array()
}
visited[main_key].push(obj)
if (typeof(borders[key]) === 'array' && visited[main_key].indeOf(key) !== -1) {
traverse(borders[key])
}
else {
//do something with the actual value
console.log(main_key, borders[key], key)
}
})
};
traverse('BRA', borders['BRA'])
Here is JSON sample: https://restcountries.eu/rest/v2/region/americas
I think my main problem is i'm struggling with transforming this JSON into a GRAPH.