Get the last item in an array

前端 未结 30 3359
执念已碎
执念已碎 2020-11-22 05:28

Here is my JavaScript code so far:

var linkElement = document.getElementById(\"BackButton\");
var loc_array = document.location.href.split(\'/\');
var newT =         


        
30条回答
  •  天命终不由人
    2020-11-22 05:55

    Just putting another option here.

    loc_array.splice(-1)[0] === 'index.html'
    

    I found the above approach more clean and short onliner. Please, free feel to try this one.

    Note: It will modify the original array, if you don't want to modify it you can use slice()

    loc_array.slice(-1)[0] === 'index.html'
    

    Thanks @VinayPai for pointing this out.

提交回复
热议问题