Listas: muchos datos juntos
Una variable guarda una cosa. Una lista guarda todas las que quieras.
Lists: many values at once
A variable holds one thing. A list holds as many as you want.
Imagina que quieres guardar los nombres de tres jugadores. Con lo que sabes harías tres variables. ¿Y si son cincuenta?
Una lista —en JavaScript se llama array— es una sola variable que
guarda muchos valores en orden. Se escribe entre corchetes [ ], separando con comas.
Imagine storing the names of three players. With what you know you would make three variables. And if there are fifty?
A list — in JavaScript called an array — is a single variable
holding many values in order. Written between brackets [ ], separated by commas.
.length es la cantidad de elementos. No es una función,
así que va sin paréntesis.
.length is how many items there are. It is not a
function, so no brackets.
Acá está la cosa que más confunde al principio: el primer elemento es el número 0, no el 1.
["Ana", "Joaco", "María"] 0 1 2
Por eso el último elemento siempre es length - 1. Y por eso los bucles del
tutorial anterior empezaban en 0: fue pensado para calzar con esto.
Here is the thing that confuses everyone at first: the first item is number 0, not 1.
["Ana", "Joaco", "María"] 0 1 2
That is why the last item is always length - 1. And why the loops in the
previous tutorial started at 0: it was designed to line up with this.
Acá se juntan las listas con lo que ya sabes de bucles, y es donde de verdad sirven.
La forma corta es for...of: se lee "para cada jugador de la lista".
Here lists meet what you know about loops, and this is where they earn their keep.
The short form is for...of: read it as "for each player in the list".
Las listas traen herramientas incorporadas. Estas cuatro son las que vas a usar siempre:
Lists come with built-in tools. These four are the ones you will always use:
Fíjate que push y pop
modifican la lista original. No devuelven una copia: cambian la que tenías.
Note that push and pop
modify the original list. They do not return a copy: they change the one
you had.
Crear listas, pedir un elemento por su posición, que se cuenta desde cero, recorrerlas con
for...of y las cuatro operaciones básicas.
Falta una forma más de guardar datos, y con esa cierras las bases: los objetos.
Creating lists, getting an item by position, counting from zero, looping with
for...of, and the four basic operations.
One more way of storing data and the foundations are complete: objects.