0
Hours
0
Minutes
0
Seconds

Create a Flat version of a nested Object | Breadcrumbs Computation | JavaScript Interview Question

@Yomesh Gupta
14

In many websites like E-commerce, Dashboards, etc there are breadcrumbs that users use to navigate between pages. This question is around that only. Suppose, you are given a nested object from the backend and you need to create a flat version of the object that can be used for breadcrumbs.

const data = {
  name: 'Devtools Tech',
  channel: {
    youtube: {
      link: 'bit.ly/devtools-yt',
      name: 'Devtools Tech',
      subscribe: "true"
    },
    platform: {
      link: 'devtools.tech',
      resources: {
        pages: ['/questions', '/resources']
      },
    }
  }
}

const output = transform(data, 'data');
console.print(output);

/**
  {
    data_name: 'Devtools Tech',
    data_channel_youtube_link: 'bit.ly/devtools-yt',
    data_channel_youtube_name: 'Devtools Tech',
    data_channel_youtube_subscribe: 'true',
    data_channel_platform_link: 'devtools.tech',
    data_channel_platform_resources_pages: [ '/questions', '/resources' ]
  }
**/

Loading IDE...