DEV Community

loading...

Discussion on: Need help cleaning up this code, which modifies the subdomain.

Collapse
elanid profile image
Daniel J Dominguez • Edited

Well, according to the documentation for slice, using a negative index will allow you start indexing from the end of the array rather than the beginning.
So domainParts.slice(domainParts.length - 2, domainParts.length) is equivalent to domainParts.slice(-2).
Another thing is that since admin is a constant, you can add that when you are concatenating the string rather than manipulating the array.

var url = window.location;

// example domain would have a subdomain so https://auth.domain.ext
var domain = url.host;

var subdomain = 'admin';
var mainDomain = domain.split('.').slice(-2).join('.');
var fullAdminDomain = url.protocol + '//' + subdomain + '.' + mainDomain;

Array.prototype.slice

Collapse
dechamp profile image
DeChamp Author

aww yes! nice!