forked from mouredev/hello-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-array.js
105 lines (68 loc) · 1.64 KB
/
10-array.js
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
/*
Clase 3 en vídeo | 24/07/2024
Condicionales, arrays y sets
https://www.youtube.com/live/XCNjoIoO3Ws?si=3XCjdZ9r41JID-by&t=978
*/
// Array
// Declaración
let myArray = []
let myArray2 = new Array()
console.log(myArray)
console.log(myArray2)
// Inicialización
myArray = [3]
myArray2 = new Array(3)
console.log(myArray)
console.log(myArray2)
myArray = [1, 2, 3, 4]
myArray2 = new Array(1, 2, 3, 4)
console.log(myArray)
console.log(myArray2)
myArray = ["Brais", "Moure", "mouredev", 37, true]
myArray2 = new Array("Brais", "Moure", "mouredev", 37, true)
console.log(myArray)
console.log(myArray2)
myArray2 = new Array(3)
myArray2[2] = "Brais"
// myArray2[0] = "Moure"
myArray2[1] = "mouredev"
myArray2[4] = "mouredev"
console.log(myArray2)
myArray = []
myArray[2] = "Brais"
// myArray[0] = "Moure"
myArray[1] = "mouredev"
console.log(myArray)
// Métodos comunes
myArray = []
// push y pop
myArray.push("Brais")
myArray.push("Moure")
myArray.push("mouredev")
myArray.push(37)
console.log(myArray)
console.log(myArray.pop()) // Elimina el último y lo devuelve
myArray.pop()
console.log(myArray)
// shift y unshift
console.log(myArray.shift())
console.log(myArray)
myArray.unshift("Brais", "mouredev")
console.log(myArray)
// length
console.log(myArray.length)
// clear
myArray = []
myArray.length = 0 // alternativa
console.log(myArray)
// slice
myArray = ["Brais", "Moure", "mouredev", 37, true]
let myNewArray = myArray.slice(1, 3)
console.log(myArray)
console.log(myNewArray)
// splice
myArray.splice(1, 3)
console.log(myArray)
myArray = ["Brais", "Moure", "mouredev", 37, true]
myArray.splice(1, 2, "Nueva entrada")
console.log(myArray)