forked from mouredev/hello-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10-array.js
More file actions
108 lines (75 loc) · 2.66 KB
/
10-array.js
File metadata and controls
108 lines (75 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Clase 25 - Arrays
*/
/*
// Array es una colección de valores
// Tiene índices
// Tiene propiedades length
// Tiene métodos push, pop, shift, unshift, etc.
// Declaración
let myArray = [];
let myArray2 = new Array();
console.log(myArray);
console.log(myArray2);
// Inicialización
myArray = [3]; // Array con un elemento
myArray2 = new Array(3); // Array con 3 elementos undefined
console.log(myArray);
console.log(myArray2);
myArray = [1, 2, 3, 4]; // Array con 4 elementos
myArray2 = new Array(1, 2, 3, 4); // Array con 4 elementos
console.log(myArray);
console.log(myArray2);
myArray = ["Juan", "Antonio", "jaf", 37, true]; // Array con 5 elementos
myArray2 = new Array("Juan", "Antonio", "jaf", 37, true); // Array con 5 elementos
console.log(myArray);
console.log(myArray2);
myArray2 = new Array(3); // Array con 3 elementos undefined
myArray2[2] = "Juan"; // Aquí se inicializa el tercer elemento
// myArray2[0] = "Antonio"
myArray2[1] = "jaf"; // Aquí se inicializa el segundo elemento
myArray2[4] = "jaf"; // Aquí se inicializa el quinto elemento
console.log(myArray2);
myArray = []; // Array vacío
myArray[2] = "juan"; // Aquí se inicializa el tercer elemento
// myArray[0] = "Antonio"
myArray[1] = "jaf"; // Aquí se inicializa el segundo elemento
console.log(myArray);
// Métodos comunes
*/
let myArray = []; // Array vacío
// push y pop
myArray.push("Juan"); // Añade al final
console.log(myArray);
myArray.push("Antonio"); // Añade al final
console.log(myArray);
myArray.push("jaf"); // Añade al final
console.log(myArray);
myArray.push(37); // Añade al final
console.log(myArray);
console.log(myArray.pop()); // Elimina el último y lo devuelve
let valorExtraido = myArray.pop(); // Elimina el último
console.log(valorExtraido);
console.log(myArray);
// shift y unshift
console.log(myArray.shift()); // Elimina el primero y lo devuelve
console.log(myArray); // Elimina el primero
myArray.unshift("Juan", "jaf"); // Añade al principio
console.log(myArray); // Añade al principio
// length
console.log(myArray.length); // Longitud del array
// clear
myArray = [];
myArray.length = 0; // alternativa a myArray = []
console.log(myArray);
// slice
myArray = ["Juan", "Antonio", "jaf", 37, true]; // Array con 5 elementos
let myNewArray = myArray.slice(1, 3); // Copia los elementos del 1 al 3
console.log("Completo: " + myArray);
console.log("slice(1, 3): " + myNewArray);
// splice
myArray.splice(1, 3); // Elimina los elementos del 1 al 3
console.log(myArray);
myArray = ["Juan", "Antonio", "jav", 37, true]; // Array con 5 elementos
myArray.splice(1, 2, "Nueva entrada"); // Elimina los elementos del 1 al 2 y añade "Nueva entrada"
console.log(myArray);