ไปสุดท้ายของธาตุอาเรย์จาวาสคริปต์
// Method - 1 ([] operator)
const arr = [5, 3, 2, 7, 8];
const last = arr[arr.length - 1];
console.log(last);
/*
Output: 8
*/
// Method - 2 (Destructuring Assignment)
const arr = [5, 3, 2, 7, 8];
const [last] = arr.slice(-1);
console.log(last);
/*
Output: 8
*/
// Method - 3 (Array.prototype.pop())
const arr = [5, 3, 2, 7, 8];
const last = arr.slice(-1).pop();
console.log(last);
/*
Output: 8
*/
// Method - 4 (Underscore/Lodash Library)
const _ = require("underscore");
const arr = [5, 3, 2, 7, 8];
const last = _.last(arr);
console.log(last);
/*
Output: 8
*/
จาวาสคริปต์ได้สุดท้ายของธาตุอาเรย์
var foods = ["kiwi","apple","banana"];
var banana = foods[foods.length - 1]; // Getting last element
ยังไงเพื่อให้รายการสุดท้ายอยู่ในตารางคู่ลำดับจาวาสคริปต์
let nums = [1,2,3,4,5];
let lastOne = nums.pop();
// -> lastOne = 5
// -> nums = [1,2,3,4];
จาวาสคริปต์สุดท้ายของธาตุอาเรย์
let arr = ['a', 'b', 'c']
arr.slice(-1)[0] // returns last element in an array
javascipt ได้สุดท้ายของธาตุอาเรย์
const arr = [1,2,3] ;
console.log(arr[arr.length - 1]);