20

I have make this code:

var newURL = $(".list-portfolio a").attr("href"),
    pathArray = newURL.split( '/' ),
    secondLevelLocation = pathArray[0];
console.log(pathArray);

    var pathArray = pathArray[3, 4];

The pathArray value is ["http:", "", "www.mikevierwind.nl", "portfolio", "ruimzicht.html"]

How can i get the last 2 items of this array. I want that the results is portfolio/ruimzicht.html.

3
  • var last2 = pathArray[3]+'/'+pathArray[4] this could be...if you have fixed url Commented Aug 10, 2012 at 9:16
  • var pathArrayNew = pathArray[3]+"\/"+pathArray[4]; ? Commented Aug 10, 2012 at 9:17
  • You could do .prop("pathname") on the element. Commented Aug 10, 2012 at 9:17

7 Answers 7

56

You don't need any of this, you just want window.location.pathname:

> window.location.pathname
"/questions/11898626/get-items-of-the-array/11898963"

This will let you in the future have directories like "portfolio/2012/ruimzicht.html", and change domains to say "www.mikevierwind.???" without changing your code.


If you are not currently on the domain (and can't do the above), you can do it your way with a one-liner:

> pathArray.slice(-2).join('/')
"portfolio/ruimzicht.html"

But this is not future-proof like the above. To make it future-proof, you can do:

> url.split(document.domain)[1].slice(1)
"portfolio/2012/ruimzicht.html"

One would do this generally on foreign URLs when you are not currently on the domain and thus can't do window.location.pathname.

Sign up to request clarification or add additional context in comments.

Comments

2
location.href.substr(location.href.indexOf(location.host)+location.host.length)

try this! it's contains URL parameters

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.
1

You could use array length if you don't have a fixed size or number of elements.

var path = array[array.length-2]+'/'+array[array.length-1];

If you just want the path use plain JS or jQuery as they suggest you in comments.

//Plain JS
var path = window.location.pathname;
//jQuery
$(location).attr('pathname'); 

1 Comment

What I meant was accessing that property on the a element.
1

try this

console.log(new URL(document.URL));

you would get object of URL

Comments

0

You could try this

var newURL = $(".list-portfolio a").attr("href"),
    pathArray = newURL.split( '/' ),
    secondLevelLocation = pathArray[0];
console.log(pathArray);

var pathArray = pathArray[3] +'/'+ pathArray[4];

Comments

0

Or May be something like this

var newURL = $(".list-portfolio a").attr("href"),
pathArray = newURL.split( '/' ),
secondLevelLocation = pathArray.shift();

var pathArray = pathArray.join('/');

Comments

0

Short

Ninjagecko gives excelent answer - I improve it a little: you don't need use window prefix, just

location.pathname

And some alternative way to get two last array items by use at()

[ pathArray.at(-2), pathArray.at(-1) ] 

pathArray = ["http:", "", "www.mikevierwind.nl", "portfolio", "ruimzicht.html"];

result = [ pathArray.at(-2), pathArray.at(-1) ]

console.log(result.join`/`);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.