Skip to main content
added 433 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

pop() mutates the underlying list: it removes the last item (and returns it).

It seems you're looking for slice:

"www.mycompany.com/sites/demo/t1".split('/').slice(0, -1).join('/')
// -> gives: "www.mycompany.com/sites/demo"

.slice(0, -1) gives the elements of the list from the start until the end minus one item.

However, this is a very poor solution to drop the last path element from a URL. In particular, splitting a string to an array, drop the last element and join the string again is inefficient. It would be better to use lastIndexOf to find the position of the last /, and then substr to extract the part of the string before the last /.

var s = "www.mycompany.com/sites/demo/t1";
s.substr(0, s.lastIndexOf('/'));

pop() mutates the underlying list: it removes the last item (and returns it).

It seems you're looking for slice:

"www.mycompany.com/sites/demo/t1".split('/').slice(0, -1).join('/')
// -> gives: "www.mycompany.com/sites/demo"

.slice(0, -1) gives the elements of the list from the start until the end minus one item.

pop() mutates the underlying list: it removes the last item (and returns it).

It seems you're looking for slice:

"www.mycompany.com/sites/demo/t1".split('/').slice(0, -1).join('/')
// -> gives: "www.mycompany.com/sites/demo"

.slice(0, -1) gives the elements of the list from the start until the end minus one item.

However, this is a very poor solution to drop the last path element from a URL. In particular, splitting a string to an array, drop the last element and join the string again is inefficient. It would be better to use lastIndexOf to find the position of the last /, and then substr to extract the part of the string before the last /.

var s = "www.mycompany.com/sites/demo/t1";
s.substr(0, s.lastIndexOf('/'));
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

pop() mutates the underlying list: it removes the last item (and returns it).

It seems you're looking for slice:

"www.mycompany.com/sites/demo/t1".split('/').slice(0, -1).join('/')
// -> gives: "www.mycompany.com/sites/demo"

.slice(0, -1) gives the elements of the list from the start until the end minus one item.