Turn text into breadcrumb links

Asked

Viewed 32 times

0

I have two variables

var link_breadcrumb = "geral1_titulo1_parte1_conteudo1";
var texto_breadcrumb = "Geral 1 - Título 1 - Parte 1 - Conteúdo 1";

I would like to assemble the breadcrumb with the links

var breadcrumb_montado = "<a href="#geral1">Geral 1</a> / <a href="#geral1_titulo1">Título 1</a> / <a href="#geral1_titulo1_parte1">Parte 1</a> / <a href="#geral1_titulo1_parte1_conteudo1">Conteúdo 1</a>";

Does anyone know how to do this in javascritp?

  • Your question is unclear, you could explain it better?

1 answer

1


Hello would look like this basically:

var link_breadcrumb = "geral1_titulo1_parte1_conteudo1";
var texto_breadcrumb = "Geral 1 - Título 1 - Parte 1 - Conteúdo 1";


var links = link_breadcrumb.split('_');
var textos = texto_breadcrumb.split(' - ')

var breadcrumb_montado = links.map(function (link, index) {
    return "<a href=" + link + ">" + textos[index] + "</a>";
}).join(" / ");

The split it separates the string from a delimiter, returning a array.

As the two array will be the same size basically you need to go through one later with the map and get the text of the other from the index.

And and Jay only serves to concatenate the array generated, transforming into string. Since you can indicate what goes between each element.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.