-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.Array.js
More file actions
73 lines (56 loc) · 1.67 KB
/
3.Array.js
File metadata and controls
73 lines (56 loc) · 1.67 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
/*
* Author: Marc Ciruelos Santos
* Date: 19-02-2024
* Description: Array class => Examples of the Declaration, functions (length, sort, pop, push, add,
*/
// Declarartion
let myArray = [7, 1, 2, 3, 4];
console.log(myArray);
let myColors = new Array("Red", "Green", "Blue");
console.log(myColors);
let myObjectArray = [
{ brand: "Tesla", model: "X" },
{ brand: "Mercedes", model: "CLK" },
];
console.log(myObjectArray);
let myMatrix = [
[1, 4],
[3, 6],
];
console.log(myMatrix[0][0]); // 1
// Array functions
// length => 5
console.log(myArray.length);
// sort the array => [1, 2, 3, 4, 7]
console.log(myArray.sort());
// pop (remove) => 7 (last number of [1, 2, 3, 4, 7])
console.log("Element removed: " + myArray.pop());
console.log(myArray);
// push (add) => (last position [1, 2, 3, 4, 2])
console.log("Element 2 added in position: " + myArray.push(2));
console.log(myArray);
// reverse => [2, 4, 3, 2, 1]
console.log(myArray.reverse());
// Join to template
let value = 5;
const template = ["<li>", value, "</li>"].join("");
console.log("Array joined:" + template);
// Map
// Usual function
let mySQRTArray = myArray.map(function (item) {
return Math.sqrt(item);
});
console.log(mySQRTArray);
// Arrow function
mySQRTArray = myArray.map((item) => Math.sqrt(item));
console.log(mySQRTArray);
// Arrow function reduced because use item as parameter
mySQRTArray = myArray.map(Math.sqrt);
console.log(mySQRTArray);
// Filter
let result = myArray.filter((item) => item % 3 === 0);
console.log("Multiple of 3: " + result);
// Slice (cut from - to)
console.log(myArray.slice(1, 3));
console.log(myArray.slice(2, -1));
console.log("Immutable myArray => original: " + myArray);