Get the last item in an array

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

    Two options are:

    var last = arr[arr.length - 1]
    

    or

    var last = arr.slice(-1)[0]
    

    The former is faster, but the latter looks nicer

    http://jsperf.com/slice-vs-length-1-arr

提交回复
热议问题