Iterate through Javascript nested array of objects and itearate all nested links

  arrays, asynchronous, javascript, json, node.js

I have JSON data of an array of objects.
Format of JSON->

{
  pages: [
    {
      address: "page-01",
      links: ["page-02", "page-03"]
    },
    {
      address: "page-02",
      links: ["page-01"]
    },
    {
      address: "page-03",
      links: ["page-01", "page-02", "page-04"]
    }
  ]
};
    

I want to write a function, if a user-entered address value as "page-01" then the function must go through all linked pages of"page-01", here in this case "page-02", "page-03" and linked pages of "page02" and "page-03" and calling multiple linked pages must be asynchronous.
There are some conditions while traversing-
1-> Visit each valid page exactly at once.
2-> Start with any given address value in the list of pages and follow links to crawl the remaining pages in the list.
3-> If a user tries to crawl an address that does not exist then we need to push that page into an error array.

Expected Output
1-> The address of pages that are visited successfully are added
to a “Success” array.
2-> The address of pages that have already been visited are
added to a “Skipped” array.
3->The address of pages that are linked to but do not exist are
added to an “Error” array

I tried to solve using Map and storing all pages in Map. But I am not able to go through linked pages recursively.

Please help me solving this.

Source: Ask Javascript Questions

LEAVE A COMMENT