Get the last item in an array

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

    The "cleanest" ES6 way (IMO) would be:

    const foo = [1,2,3,4];
    const bar = [...foo].pop();
    

    This avoids mutating foo, as .pop() would had, if we didn't used the spread operator.
    That said, I like aswell the foo.slice(-1)[0] solution.

提交回复
热议问题