Get the last item in an array

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

    const [lastItem] = array.slice(-1);

    Array.prototype.slice with -1 can be used to create a new Array containing only the last item of the original Array, you can then use Destructuring Assignment to create a variable using the first item of that new Array.

    const lotteryNumbers = [12, 16, 4, 33, 41, 22];
    const [lastNumber] = lotteryNumbers.slice(-1);
    
    console.log(lotteryNumbers.slice(-1));
    // => [22]
    console.log(lastNumber);
    // => 22

提交回复
热议问题