Get the last item in an array

前端 未结 30 2768
执念已碎
执念已碎 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:53

    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.

    var fileName = loc_array.slice(-1)[0];
    if(fileName.toLowerCase() == "index.html")
    {
      //your code...
    }
    

    Using pop() will change your array, which is not always a good idea.

提交回复
热议问题