Home > database >  breadcrumbs from split into single string
breadcrumbs from split into single string

Time:10-26

I have a string separated by "/" which I need to make into breadcrumbs.

I have tried this, but the x is not working.

var path = "sub1/sub2/sub3";
var breadcrumbs = path.split('/');
for(var i = 0, l = breadcrumbs.length; i < l; i  ) {
    var x = path.substring(0, breadcrumbs[i].length);
    
    console.log( x );
}

x needs to be the full path from the beginning, right to the last one selected.

for example, if you were to click on "sub2" in the breadcrumbs, the value should be sub1/sub2

CodePudding user response:

One possibility is to use Array#join(): breadcrumbs.slice(0, i 1).join('/'):

var path = "sub1/sub2/sub3";
var breadcrumbs = path.split('/');
for (var i = 0, l = breadcrumbs.length; i < l; i  ) {
  var x = breadcrumbs.slice(0, i   1).join('/');

  console.log(x);
}

  • Related