// Un ejemplo de arreglo simple (Array)
var myFriend = "Sophia";
var myFriends = ["Sophia", "John", "Leif"];
// myFriends[1]
fill(255, 0, 0);
text( myFriends[0], 10, 30);
text( myFriends[1], 10, 80);
text( myFriends[2], 10, 130);
text( myFriends[3], 10, 130);
--------------------------------
Un arreglo con la función "lenght" que mide cuantos ítems hay en el arreglo.
var myFriend = "Sophia";
var myFriends = ["Sophia", "John", "Leif", "Winston", "OhNoes Guy!!"];
// myFriends[1]
fill(255, 0, 0);
text( myFriends[0], 10, 30);
text( myFriends[1], 10, 80);
text( myFriends[2], 10, 130);
text("I have " + myFriends.length + " friends!!!", 10, 150);
--------------------------------
Iteración (loop) + arreglos(Array)
var myFriends = ["Sophia", "John", "Leif", "Winston", "OhNoes Guy", "Sal"];
fill(255, 0, 0);
var friendNum = 0;
while(friendNum < myFriends.length) {
text(myFriends[friendNum], 10, 30+friendNum*30);
friendNum++;
}
/*
1. What do I want to repeat?
the text() command
2. What do I want to change each time?
the friend num, and the y position
3. How long should we repeat?
until there are no more friends left
*/
----------------
Se puede usar "while" o "for", indistintamente, para iterar arreglos
var myFriends = ["Sophia", "John", "Leif", "Winston", "OhNoes Guy", "Sal"];
fill(255, 0, 0);
var friendNum = 0;
while(friendNum < myFriends.length) {
text(myFriends[friendNum], 10, 30+friendNum*30);
friendNum++;
}
for(var friendNum = 0; friendNum < myFriends.length; friendNum++) {
text(myFriends[friendNum], 87, 30+friendNum*30);
}
-------------------------------------------------------------
Añadir nuevos items del arreglo a un array usando "Push"
var xPositions = [100, 200];
var draw = function() {
if (mouseIsPressed) {
xPositions.push(mouseX);
}
noStroke();
background(212, 254, 255);
stroke(64, 117, 207);
fill(196, 33, 255);
for (var i = 0; i < xPositions.length; i++) {
line(xPositions[i], 120, 194, 285);
ellipse(xPositions[i], 104, 32, 46);
}
var hopper = getImage("creatures/Hopper-Jumping");
image(hopper, 189, 227);
};
---------------------------------------------------------------
Ocultar el tutorial de navegación
Muchas veces queremos almacenar listas de valores cuando estamos creando programas y, en JavaScript, podemos hacer eso al usar un tipo de valor llamado un arreglo.
Para crear un arreglo, declaramos una variable como siempre lo hacemos, pero luego rodeamos nuestra lista de valores con corchetes y separamos cada valor con una coma:
var xPositions = [33, 72, 64];
Podemos guardar cualquier tipo de valor de JavaScript en un arreglo, no solo números. Aquí hay un ejemplo donde almacenamos una lista de cadenas de caracteres:
var myFriends = ['Winston', 'OhNoesGuy', 'John', 'Sophia'];
Muchas veces queremos desplegar la longitud de un arreglo, o hacer algo con base en la longitud del arreglo. Afortunadamente, cada arreglo tiene una propiedad length que nos dirá la longitud actual del arreglo:
text(myFriends.length, 200, 200); // Despliega "4"
Cuando queremos acceder a un valor particular en un arreglo, accedemos a él haciendo referencia a su "índice" en el arreglo, el cual representa su posición. El primer índice en un arreglo es "0", así que si queremos acceder al primer elemento en un arreglo, especificamos el nombre de la variable del arreglo, luego corchetes y 0:
text(myFriends[0], 200, 0); // Despliega "Winston"
El segundo elemento está en el índice "1", el tercero en el índice "2" y el cuarto en el índice "3":
text(myFriends[1], 200, 100); // Despliega "OhNoesGuy" text(myFriends[2], 200, 200); // Despliega "John" text(myFriends[3], 200, 300); // Despliega "Sophia"
La indexación basada en cero es uno de los aspectos más confusos de los arreglos para los programadores nuevos, así que ten eso en cuenta si apenas estás empezando con arreglos. ¡Eventualmente te vas a acostumbrar !
A menudo queremos hacer alguna acción para cada elemento en un arreglo, como cuando arriba usamos el comando text() para desplegar los nombres. En lugar de escribir el código una y otra vez, es mejor usar un bucle for para iterar a través de cada uno de los elementos en el arreglo, y hacerle algo a cada elemento dentro del bucle. Tenemos que empezar desde el índice 0, ir hasta llegar al final del arreglo, y agregarle 1 al índice cada vez. Aquí está cómo haríamos eso:
for (var i = 0; i < myFriends.length; i++) { text(myFriends[i], 200, i*100); }
Observa cómo ponemos i dentro de los corchetes, porque representa el índice actual cada vez que se ejecuta el bucle.
Los arreglos pueden ser cambiados de muchas maneras. Para empezar, podemos cambiarles un valor:
myFriends[1] = "TheErrorBuddy";
También podemos agregarles valores completamente nuevos, usando el método push(), pasándole el nuevo valor:
myFriends.push("Hopper");
Después de ejecutar esa línea de código, la propiedad length de nuestro arreglo cambiará para reflejar la nueva longitud, y el índice final del arreglo será 4 en lugar de 3.
Si quieres una lista completa de lo que puedes hacer con los arreglos en JavaScript, revisa esta referencia.
--------------------------------------------------------------------------------------------------------------
Los Mejores ejemplo derivados de arrays
Lilli S
Filling in the Gaps B1: All About Arrays
Noble Mushtak
Kyle Woodbury
T#1B12P (T#1.57ηP) (Bio updated 6/3)
Sean
88 votos • 27 derivados
90 votos • 19 derivados
16 votos • 2 derivados
64 votos • 42 derivados
6 votos • 3 derivados
5 votos • 3 derivados
4 votos • 4 derivados
4 votos
4 votos • 5 derivados
3 votos • 6 derivados
3 votos
3 votos • 1 derivado
3 votos
2 votos • 1 derivado
2 votos
2 votos
2 votos • 1 derivado
2 votos
redthumb.liberty
DorivalMartinez
Pride724
SerendipityBath
emilyplays2
a.schemaitat
2 votos
1 voto • 1 derivado
1 voto
1 voto
1 voto
1 voto
LaylaVC
Kyra Yehle
VinylBuda
mrl19
ultimatek
1 voto
1 voto
1 voto • 3 derivados
1 voto
1 voto • 2 derivados
1 voto
nate.robbins77
wish126
Andy Leeds
EversonJoseph2019
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
catlover
catlover
Sarah Davis
Tyler
mahantesh
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto • 1 derivado
1 voto • 1 derivado
1 voto
1 voto
T#1B12P (T#1.57ηP) (Bio updated 6/1)
Vatsal
faz919
Seth Schaffer
Jesse Harper
Spin-off of "var array = [0, 1, 2, 3, 4];"
Andi York Glassford
1 voto
1 voto
1 voto
1 voto • 1 derivado
1 voto
1 voto
Brian C
Spin-off of "var array = [0, 1, 2, 3, 4];"
reachfreddie
5picklefamily
lindseymcal
Spin-off of "var array = [0, 1, 2, 3, 4];"
priyansh0323
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
ReyRout
Annie Andrews
Spin-off of "var array = [0, 1, 2, 3, 4];"
fusionartbar
Messing with Arrays and randomisation
david alberts
Spin-off of "var array = [0, 1, 2, 3, 4];"
Daniel Walley
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
Andrew Gazelka
christopher.behrens
Derivação de "Arrays | JavaScript"
bithiagodavila
Empesando desde abajo hasta arriba
jayson garcia
Spin-off of "Arrays | JavaScript"
zacharyvenditti
Spin-off of "Arrays | JavaScript"
Elizabeth
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
bolognesec
Salamander Khan
Spin-off of "Arrays | JavaScript"
Christopher-lee Merrill
Spin-off of "Arrays | JavaScript"
Emerald Sketch
Blaze Runner™
Mikita Hill-Cashaw
1 voto • 1 derivado
1 voto
1 voto • 1 derivado
1 voto
1 voto
1 voto
Spin-off of "var array = [0, 1, 2, 3, 4];"
all.london12
Derivação de "Arrays | JavaScript"
biellentmunhoz
18lukem
Spin-off of "var array = [0, 1, 2, 3, 4];"
shane912048s
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
rg18748
Gráfico de barras - Derivado de "Arrays | JavaScript"
rafael andres messina
Spin-off of "Arrays | JavaScript"
William Knowles-Kellett
Spin-off of "var array = [0, 1, 2, 3, 4];"
Gabriella Chantal Gittens
Orlando Melendrez
Spin-off of "var array = [0, 1, 2, 3, 4];"
Damian Matthews
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto • 1 derivado
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
Spin-off of "var array = [0, 1, 2, 3, 4];"
BrianAndrewGui
Hector Franco
karandip15
josephhand6200
Spin-off z "Arrays | JavaScript"
dawid.pasler
Spin-off of "var array = [0, 1, 2, 3, 4];"
ZeenatMaqbool
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
gregorylallen
Daniel Mills
TimothyTuininga
Spin-off of "var array = [0, 1, 2, 3, 4];"
kourtnyy lewis
Spin-off of "Arrays | JavaScript"
Fred Taylor
1 voto • 1 derivado
1 voto
1 voto
1 voto
1 voto
1 voto • 1 derivado
Spin-off of "Arrays | JavaScript"
Bhbtuzb Sbwvsj
Spin-off of "var array = [0, 1, 2, 3, 4];"
Deven Stevenson
karandip15
frangarcia000
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
Spin-off of "var array = [0, 1, 2, 3, 4];"
barcenas0005
Spin-off of "Arrays | JavaScript"
gisli.hjalmtysson
Spin-off of "var array = [0, 1, 2, 3, 4];"
Jackmanchan
Spin-off of "Arrays | JavaScript"
Steven Thompson
Michelle Wang
Spin-off of "Arrays | JavaScript"
hondlfamily
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
Spin-off of "var array = [0, 1, 2, 3, 4];"
olga nelson
Derivação de "Arrays | JavaScript"
larissapaixao15
K Vincent
Bern de Veld
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
Spin-off of "var array = [0, 1, 2, 3, 4];"
bindnerjonathan
Spin-off of "var array = [0, 1, 2, 3, 4];"
neilkapoor11
nate hacker
Spin-off of "var array example
c.mcneile
1 voto
1 voto
1 voto • 1 derivado
1 voto
1 voto
1 voto
Spin-off of "var array = [0, 1, 2, 3, 4];"
http://facebookid.khanacademy.org/774675509259470
Spin-off of "Arrays | JavaScript"
Bradley Ray Loomis
Derivação de "var array = [0, 1, 2, 3, 4];"
gabriel.figueredo1996
Orlando Melendrez
Spin-off of "Arrays | JavaScript"
naama.b.22628
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
Hayden
Derivação de "var array = [0, 1, 2, 3, 4];"
lizasantos.linda
Paul Ramnora
Spin-off of "var array = [0, 1, 2, 3, 4];"
lesmorgan37
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
antoshevska
Kristi Fjell
pedrosolppc
Spin-off of "Arrays | JavaScript"
lejeunedeacutissc
Spin-off of "var array = [0, 1, 2, 3, 4];"
mahdiWHEELER
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
Anthony Michael Hanson
Spin-off of "Arrays | JavaScript"
Oliver Isenrich
Spin-off of "var array = [0, 1, 2, 3, 4];"
Russell Salacup
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto
1 voto • 1 derivado
1 voto
1 voto • 4 derivados