I have an array:
aNumeros=[1, 5, 6, 7, 8, 9, 10, 12];
I want to create another array from that, containing the last 5 numbers:
aNuevo= [7, 8, 9, 10, 12]
Thanks in advance
I have an array:
aNumeros=[1, 5, 6, 7, 8, 9, 10, 12];
I want to create another array from that, containing the last 5 numbers:
aNuevo= [7, 8, 9, 10, 12]
Thanks in advance
You can do it using the splice function, passing as a parameter the position from where you want to cut.
In this case, as the starting position, we pass the length of the array (aNumbers) and subtract 5, so we will always look for the last 5.
Take a look at the documentation, to see how it works in more detail https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Objetos_globales/Array/splice
In order to respond to your concern, I share the following code segment, I hope it will be useful, greetings:
A solution similar to that of Eugeni Bejan , put -value and it will return what is indicated, it is not necessary to obtain the length