Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JQuery breadcrumbs from single string

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

One possibility is 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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading