-1

I need an efficient function to extract the index and the position of an argument in an array - but the array is could be complex.

This is the array:

let root_menu = {
        nav_title: $rootScope.t['General administration'],
        items: [
          { // overview
            title: $rootScope.t['Overview'],
            path: `/root/overview`,
            module: "overview/menu",
            icon: "fa fa-home"
          }, // overview
          { // cardboards
            title: $rootScope.t['Cardboards'],
            path: `/root/cardboards`,
            module: "cardboards/menu",
            icon: "fa fa-files-o",
            subs: [
              {
                title: $rootScope.t['Suppliers'],
                path: `/root/cardboards/suppliers`,
                module: "suppliers/menu",
                icon: "fa fa-handshake-o"
              },
              {
                title: $rootScope.t['Employees'],
                path: `/root/cardboards/employees`,
                module: "employees/menu",
                icon: "fa fa-address-book-o"
              },
            ]
          }, // cardboards
          { // charts
            title: $rootScope.t['Charts'],
            path: `/root/charts`,
            icon: "fa fa-area-chart",
            module: "charts/menu",
            subs: [
              {
                title: $rootScope.t['Activity'],
                path: `/root/charts/activity`,
                module: "charts/activity/menu"
              },
            ]
          }, // charts
          { // settings
            title: $rootScope.t['Settings'],
            path: `/root/settings`,
            module: "settings/menu",
            icon: "fa fa-cogs",
            subs: [
              {
                title: $rootScope.t['Permissions'],
                path: `/root/settings/permissions`,
                module: "settings/permissions/menu",
                icon: "fa fa-file-text-o"
              }
            ]
          } // settings
        ]
      };

Now I have this breadcrumb array:

["/", "/root", "/root/cardboards", "/root/cardoards/employees", "/root/cardboards/employees/123"]

And I want to match each key (if any) in the breadcrumbs array to the first array, so I can have something like this:

[
 {path: "/root/cardboards/", title: "the title from the first 
 array"},
 {path: "/root/cardboards/employees", title: "the title from the . 
 first array"}
]

As you can see, if there is no match between the second and the first array (under path key), there should not be attachment to the new array.

How can I do this efficiently - better with ES6.

3
  • 1
    You can take a look at my solution to a very similar question or the other answers on this question for a reference. After looking at that you should be able to get this working. Commented Nov 9, 2018 at 21:45
  • Please show what you have tried. Stackoverflow isn't a free code writing service. Do some research into recursive tree walking functions Commented Nov 9, 2018 at 21:55
  • If not an exact dup, a big help anyway. Commented Nov 9, 2018 at 22:04

1 Answer 1

0

One solution can be to create a mapping from path to its object. But to do that, you need to travel your items array recursively. Once you have that index/mapping, you can get the title of the path easily by accessing the value of that path key in the mapping:

let items = [{ // overview
    title: 'Overview',
    path: `/root/overview`,
    module: "overview/menu",
    icon: "fa fa-home"
  }, // overview
  { // cardboards
    title: 'Cardboards',
    path: `/root/cardboards`,
    module: "cardboards/menu",
    icon: "fa fa-files-o",
    subs: [{
        title: 'Suppliers',
        path: `/root/cardboards/suppliers`,
        module: "suppliers/menu",
        icon: "fa fa-handshake-o"
      },
      {
        title: 'Employees',
        path: `/root/cardboards/employees`,
        module: "employees/menu",
        icon: "fa fa-address-book-o"
      },
    ]
  }, // cardboards
  { // charts
    title: 'Charts',
    path: `/root/charts`,
    icon: "fa fa-area-chart",
    module: "charts/menu",
    subs: [{
      title: 'Activity',
      path: `/root/charts/activity`,
      module: "charts/activity/menu"
    }, ]
  }, // charts
  { // settings
    title: 'Settings',
    path: `/root/settings`,
    module: "settings/menu",
    icon: "fa fa-cogs",
    subs: [{
      title: 'Permissions',
      path: `/root/settings/permissions`,
      module: "settings/permissions/menu",
      icon: "fa fa-file-text-o"
    }]
  } // settings
];


function flatten(tree) {
  let res = [];
  function traverse(arr) {
    res.push(...arr);
    arr.forEach(o => traverse(o.subs || []));
  }
  traverse(tree);
  return res;
}

let pathIndex = flatten(items).reduce((acc, curr) => {
  if (curr.path) acc[curr.path] = curr;
  return acc;
}, {});

let breadcrumbs = ["/", "/root", "/root/cardboards", "/root/cardboards/employees", "/root/cardboards/employees/123"];
console.log(breadcrumbs.filter(path => path in pathIndex)
                       .map(path => ({title: pathIndex[path].title, path})));
                       

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.