EN
TypeScript - shift array elements
0
points
In this article, we would like to show you how to shift array elements in TypeScript.
Quick solution:
const letters: string[] = ['A', 'B', 'C', 'D'];
letters.shift(); // removes 'A' from letters array
console.log(letters); // [ 'B', 'C', 'D' ]
Practical example
The shift() method removes the first element from an array and returns that removed element, so you can use it later.
const letters: string[] = ['A', 'B', 'C', 'D'];
const shifted: string = letters.shift(); // shifts 'A' from letters array to shifted const
console.log(letters); // B,C,D
console.log(shifted); // A
Output:
[ 'B', 'C', 'D' ]
A