简体   繁体   中英

TypeError: Cannot call method 'replace' of undefined at compareResults

function generateBC(url, separator) {
var splitthis = url.split("/");
var MiddleBit = [];
var RemoveFirstElement = splitthis.shift(); 
var RemoveLastElement  = splitthis.pop(); 
var RemoveLastElementDot = RemoveLastElement.substring(0, RemoveLastElement.indexOf('.')).toUpperCase();
var arrayLength = splitthis.length;
for (var i = 0; i < arrayLength; i++) {
 var elementOk =  splitthis[i].toUpperCase();
var urlOk = "<a href='/pictures/'>" + elementOk + "</a>";
  MiddleBit.push(urlOk);
 }
var ConMiddleBitS = String(MiddleBit).replace(/,/g , separator);
var completed = '<a href="/">HOME</a> ' + separator + ConMiddleBitS + separator + "<span class='active'>" + RemoveLastElementDot + "</span>" ;
document.write(completed);
}
generateBC("mysite.com/pictures/hotels/tens/holidays.html", " : ");

I don't know why I get

TypeError: Cannot call method 'replace' of undefined at compareResults` on .replace() ?

Can someone please explain why, as I see nothing wrong with the above.

Thank-you!

It seems that you're trying to use a String method on an array. Have you tried joining the array and the using the replace() method?

var ConMiddleBitS = MiddleBit.join('').replace(/,/g , separator);

EDIT:

If you're trying to remove the , from the array you don't have to use replace, you can just do MiddleBit = MiddleBit.join(separator) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM