Js ได้คนสุดท้ายของธาตุอาเรย์

รหัสตัวอย่าง

99
0

ไปสุดท้ายของธาตุอาเรย์จาวาสคริปต์

// 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
*/
91
0

จาวาสคริปต์ได้สุดท้ายของธาตุอาเรย์

var foods = ["kiwi","apple","banana"];
var banana = foods[foods.length - 1]; // Getting last element
27
0

จาวาสคริปต์ได้เมื่อรายการที่อยู่ในตารางคู่ลำดับ

var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
25
0

js เอารายการสุดท้ายในอาเรย์

const heroes = ["Batman", "Superman", "Hulk"];
const lastHero = heroes.pop(); // Returns last elment of the Array
// lastHero = "Hulk"

หน้าคล้ายกัน

คล้ายกันหน้ากับตัวอย่าง

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังหน้ากับตัวอย่างอยู่ในหมวดหมู่