0

I have the following URL: http://myshop.dev/member/message/inbox/detail/58c4fad6607a1d16f0006b3b

I want to remove the last two parts, so this would produce: http://myshop.dev/member/message/inbox

I tried like this :

var vars = "http://myshop.dev/member/message/inbox/detail/58c4fad6607a1d16f0006b3b";
var arrVars = vars.split("/");
var lastVar = arrVars.pop();
var restVar = arrVars.join("/");
console.log(restVar);

It only successfully removes the last part.

I want to remove one more part.

How can I do that?

trincot
  • 317,000
  • 35
  • 244
  • 286
samuel toh
  • 6,836
  • 21
  • 71
  • 108

4 Answers4

3

Why don't you simply use the substring function :

if (vars.toLowerCase().indexOf("/detail/")!=-1)
    restVar = vars.substring(0,vars.toLowerCase().indexOf("/detail/"))

I think it gives you more control on what you are doing.

Matteo Conta
  • 1,423
  • 13
  • 17
2

Just pop it again. :)

var vars = "http://myshop.dev/member/message/inbox/detail/58c4fad6607a1d16f0006b3b";
var arrVars = vars.split("/");
arrVars.pop();
arrVars.pop();
var restVar = arrVars.join("/");
console.log(restVar);

Or you could just use splice:

var vars = "http://myshop.dev/member/message/inbox/detail/58c4fad6607a1d16f0006b3b";
var arrVars = vars.split("/");
arrVars.splice(-1,2)
var restVar = arrVars.join("/");
console.log(restVar);
Marcin Restel
  • 280
  • 3
  • 13
1

You could pop twice, but there are other solutions.

For instance, decreasing the array's length with 2 has the same effect:

arrVars.length -= 2;

var vars = "http://myshop.dev/member/message/inbox/detail/58c4fad6607a1d16f0006b3b";
var arrVars = vars.split("/");
arrVars.length -= 2;
var restVar = arrVars.join("/");
console.log(restVar);

But maybe consider using a regular expression:

vars.match(/(.*)\/.*\//)[1]

This is saying: grab as much as possible, as long as two forward slashes still come after it.

var vars = "http://myshop.dev/member/message/inbox/detail/58c4fad6607a1d16f0006b3b";
var restVar = vars.match(/(.*)\/.*\//)[1];
console.log(restVar);

Note that the input should be a valid URL. If there are no two forward slashes in your string, this will raise an exception. If you need to deal with this, and don't like try...catch then first test whether the match result is not null, and only then get the captured group content:

var restVar = vars.match(/(.*)\/.*\//);
restVar = restVar && restVar[1];
trincot
  • 317,000
  • 35
  • 244
  • 286
0

Instead of calling pop() twice you can use splice() or slice() like this:

splice() - w3schools docs

var vars = "http://myshop.dev/member/message/inbox/detail/58c4fad6607a1d16f0006b3b";
var arrVars = vars.split("/");

// remove desired # of URL pieces here in 2nd parameter:
arrVars.splice(-2, 2);
var restVar = arrVars.join("/");

slice() - w3schools docs

var vars = "http://myshop.dev/member/message/inbox/detail/58c4fad6607a1d16f0006b3b";
var arrVars = vars.split("/");
var arrLength = arrVars.length;

// remove desired # of URL pieces here:
var arrVars = arrVars.slice(0, arrLength - 2);
var restVar = arrVars.join("/");
Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59
  • This is the wrong way to use `splice`, which mutates `arrVars` already by itself. Either use `slice`, or use `splice` without capturing the return value, but then of course with different argument values. – trincot Mar 12 '17 at 08:59