Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Get last item from array ts

const arr: string[] = ['a', 'b', 'c'];

V1
const last = arr.at(-1);
console.log(last); // -> "c"

V2
const last = arr[arr.length - 1];
console.log(last); // -> "c"

V3
If you don't need the array afterwards, you could use  
const last = arr.pop()
console.log(last); // > "c"

V4
const last = arr.slice(-1)[0]
console.log(last); // -> "c"
Comment

how to get last element of array in typescript

var items: String[] = ["tom", "jeff", "sam"];

alert(items[items.length-1])
Comment

js get last n elements of array

const cars = ["benz", "bmw", "volvo", "skoda"];

const lastThree = cars.slice(-3);

console.log(lastThree); // ["bmw", "volvo", "skoda"]
Comment

PREVIOUS NEXT
Code Example
Typescript :: array of objects for in 
Typescript :: validation maxlength angular 
Typescript :: java list of objects example 
Typescript :: how to check if its a character in r 
Typescript :: mysqli_select_db expects 2 parameters 
Typescript :: after effects free download 
Typescript :: loop through string typescript 
Typescript :: typescript remove an item from array 
Typescript :: python find the number of elements in a list 
Typescript :: fetch in ts 
Typescript :: transport unknown socket.io 
Typescript :: typescript ge t current screen resolution 
Typescript :: React & TypeScript Chrome Extension Development [2021] 
Typescript :: google fonts for flutte 
Typescript :: import openzeppelin contracts in remix 
Typescript :: ts class 
Typescript :: nodejs exec exit code 
Typescript :: declare jquery in typescript 
Typescript :: how to check if there is any point which lies inside the circle 
Typescript :: argument of type * is not assignable to parameter of type SetStateAction 
Typescript :: typescript tsconfig.json file 
Typescript :: angular get user location 
Typescript :: how to declare variable in typescript 
Typescript :: DAX check if value exists in another table 
Typescript :: extend typescript 
Typescript :: increase space between border dots css 
Typescript :: access single document with its id flutter 
Typescript :: Contract in ethers.js 
Typescript :: arrow function in ts 
Typescript :: nuxt 3 postcss 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =