Get the last item in an array

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

    Whatever you do don't just use reverse() !!!

    A few answers mention reverse but don't mention the fact that reverse modifies the original array, and doesn't (as in some other language or frameworks) return a copy.

    var animals = ['dog', 'cat'];
    
    animals.reverse()[0]
    "cat"
    
    animals.reverse()[0]
    "dog"
    
    animals.reverse()[1]
    "dog"
    
    animals.reverse()[1]
    "cat"
    

    This can be the worst type of code to debug!

提交回复
热议问题