retourner les deux dernières valeurs du tableau en javascript
arr.slice(Math.max(arr.length - 5, 1))
Fancy Fox
arr.slice(Math.max(arr.length - 5, 1))
// array.slice(start, end)
const FRUITS = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = FRUITS.slice(1, 3);
// citrus => [ 'Orange', 'Lemon' ]
// Negative values slice in the opposite direction
var fromTheEnd = FRUITS.slice(-3, -1);
// fromTheEnd => [ 'Lemon', 'Apple' ]
var oldColors=["red","green","blue"];
var newColors = oldColors.slice(); //make a clone/copy of oldColors
arr.slice(Math.max(arr.length - 5, 0))
const ar = [1, 2, 3, 4, 5];
// slice from 1..3 - add 1 as the end index is not included
const ar2 = ar.slice(1, 3 + 1);
console.log(ar2);
Concept:
s[x:y:z] , x- left index , y-1 - right index
z is the number of steps of jumping between 2 chr
if z>0 :
direction of jump is right to left(R2L)
else:
direction of jump is left to right(L2R)
s[:3] means x=0,y=2,z=1
s[3:] means x=3,y=len(s)-1,z=1
examples:
s="dbdhdhndnbdd"
print(s[2:6:2])
//prints dd as left index is 2 right is 5 -> substring is dhdh & 2 means d,skip h,then d
s="djdjcjfd"
print(s[::-2])
//prints djjj, left index is 0 ,right is len(s)-1 -> substring is djdjcjfd &
//-2 means start from right side in substring in steps of 2-> d,skip f,then j,skip c,then j & j