I was wondering if there is a way to do this in JavaScript:
# Lenguaje Python
arr = [1,2,3,4,5,6,7,8,9,0]
for i in arr[::-1]: # ej: imprimir lista de forma invertida/al revés
print(i)
Does the syntax array[::]
exist in JavaScript, could it be achieved in a similar way or only under one method?
Thanks to the answers, but I'm not just looking for a way to reverse the order of an array, that was an example of what can be done in the syntax array[::]
, there are others like array[:-1]
which will iterate all except the last, array[:3]
which will iterate only the first 3 elements, etc.
I also accept solutions with jQuery
Javascript doesn't have that sugar syntax for the for loop. The method exists
.forEach()
, but it will always traverse the array in an increasing direction. Similarly you havefor(let i of array) {...}
to loop through iterables (which would be the closest to what you're looking for, but you can't reverse it)If you don't mind destroying the array, you can do the following:
A third option would be to add a new method to Array that is the equivalent of .forEach() but in reverse:
In general, you have to use a normal for loop to traverse an array or part of it:
Although there are many methods to work with them: forEach, reduce, map, some, every, filter ...
In javascript you can use
reverse()
. It reverses the order of the elements.I think what you are looking for is the
slice ()
. Returns the selected elements in an array, as a new array object.The method
slice ()
selects elements beginning at the given start argument, and ending at, but not including, the given end argument.Ex:
If what you want is to print the data in reverse, I leave you the code as an explanation so that the syntax is understood:
To achieve this, you just have to get the length of the array with the length command and just subtract 1 from it (arrays start from position 0, that's why we subtract 1 to get the position of the last element)
Explanation: You iterate an array from a position
s
to a positionf
(inclusive) and exclude an array of elements, which is ,exclude
and a function to be executed, which isfunc
.And you use it like this, I want to print an array
["A", "B", "C", "D", "E"]
, from index0
to index3
and also skip theB
, which is at index2
.This should show me:
A C D
, and check that it works:There is no syntax
array[::]
, much lessarray[:-1]
orarray[:3]
.The only way you could achieve something similar would be through the filter methods and the forEach itself .
Both methods have the same parameters in their callback:
currentValue
,index
andarray
(Read the documentation for more information about them).Both differ in that the filter method will create a new array as long as its true condition is met. While the forEach will do what you ask regardless of whether it is true or false, plus it will overwrite the array.
I leave you some examples of what I tell you. Note that there is a filter that does it for the current element while another one does it for index (specific position of the array).
You can use the reverse() method in javascript, it will order them backwards
arr = [1,2,3,4,5,6,7,8,9,0];
arr.reverse();