Here is my JavaScript code so far:
var linkElement = document.getElementById("BackButton");
var loc_array = document.location.href.split('/');
var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-2])));
linkElement.appendChild(newT);
Currently it takes the second to last item in the array from the URL. However I want to do a check for the last item in the array to be "index.html" and if so, grab the third to last item instead.
In ECMAScript proposal Stage 1 there is a suggestion to add an array property that will return the last element: proposal-array-last.
Syntax:
You can use polyfill.
Proposal author: Keith Cirkel(chai autor)
Here's how to get it with no effect on the original ARRAY
If you use pop(), it will modify your array
But you can use this so it has no effect on the original array:
Using lodash _.last(array) Gets the last element of array.
Getting the last item of an array can be achieved by using the slice method with negative values.
You can read more about it here at the bottom.
Using pop() will change your array, which is not always a good idea.
I'll suggest to create helper function and reuse it every time, you'll need it. Lets make function more general to be able to get not only last item, but also second from the last and so on.
Usage is simple
Now, lets solve the original issue
Next line does the job
So, you'll need to rewrite
in this way
or use additional variable to increase readability
I think if you only want get the element without remove, is more simple use this:
by the way... i didnt check performance, but i think is more simple and clean to write