Get the last item in an array

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

    ES6 object destructuring is another way to go.

    const {length, [length-1]: last}=[1,2,3,4,5]
    console.log(last)

    You extract length property from Array using object destructuring. You create another dynamic key using already extracted key by [length-1] and assign it to last, all in one line.

提交回复
热议问题