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:
xxxxxxxxxx
1
const letters: string[] = ['A', 'B', 'C', 'D'];
2
letters.shift(); // removes 'A' from letters array
3
4
console.log(letters); // [ 'B', 'C', 'D' ]
The shift()
method removes the first element from an array and returns that removed element, so you can use it later.
xxxxxxxxxx
1
const letters: string[] = ['A', 'B', 'C', 'D'];
2
const shifted: string = letters.shift(); // shifts 'A' from letters array to shifted const
3
4
console.log(letters); // B,C,D
5
console.log(shifted); // A
Output:
xxxxxxxxxx
1
[ 'B', 'C', 'D' ]
2
A