Repository navigation
Expand file tree
/
Copy patharray.mjs
More file actions
108 lines (98 loc) · 2.89 KB
/
Copy patharray.mjs
File metadata and controls
108 lines (98 loc) · 2.89 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
//Creating an array
const array1 = new Array(1, 2, 3, 4, 5, 6);
const array2 = Array(1, 2, 3, 4, 5, 6);
const array3 = [1, 2, 3, 4, 5, 6];
console.log("Array 1:", array1);
console.log("Array 2:", array2);
console.log("Array 3:", array3);
//Creating an array with non-zero length but without any elements
const length = 10;
const array4 = Array(length);
const array5 = new Array(length);
const array6 = [];
//const arr = Array(5.1); RangeError: Invalid array length
console.log("Array 4:", array4);
console.log("Array 5:", array5);
console.log("Array 6:", array6);
//Assigning array to an object as property
const employees = {
names: ["Mahedi", "Hassan", "Shawon"],
};
employees.id = [1, 2, 3];
console.log("Employee Information: ", employees);
//Using Array.of();
const array7 = Array.of(5.1);
console.log("Array 7:", array7);
//Accessing array elements
console.log(array1[0]);
console.log(array1[3]);
console.log(array1["length"]);
//Populating an array
const student = [];
student[0] = "Mahedi";
student[1] = "Hassan";
student[2] = "Shawon";
student[3.1] = "Chief";
console.log(student);
let teacher = new Array("teacher1", "teacher2", "teacher3");
console.log(teacher);
//Array length checking
console.log("Array length of student is:", student.length);
//Truncating array by using the length property
const staff = ["Mahedi", "Hassan", "Shawon"];
console.log("Staff array length:", staff.length);
console.log("Staffs :", staff);
staff.length = 2;
console.log("Staffs :", staff);
//Iterating over arrays
//Method 1
let nums = [1,2,3,4,5,6,7,8,9,10];
for(let i =0; i<nums.length; i++)
{
console.log(nums[i]);
}
//Method 2
nums = [1,,3,,5];
nums.forEach(num=> {
console.log(num);
});
//Array methods
//concat()
const totalEmployees = teacher.concat(staff);
console.log("Total employees after concat(): ",totalEmployees);
//join()
const list = totalEmployees.join("*");
console.log("Total employees after join(): ",list);
//push()
totalEmployees.push("Potato");
console.log("Total employees after push(): ",totalEmployees);
//pop()
totalEmployees.pop();
console.log("Total employees after pop(): ",totalEmployees);
//shift()
totalEmployees.shift();
console.log("Total employees after shift(): ",totalEmployees);
//unshift()
totalEmployees.unshift('teacher1');
console.log("Total employees after unshift(): ",totalEmployees);
//slice()
const sliced = totalEmployees.slice(1, 4);
console.log("Sliced: ",sliced );
//at()
console.log(sliced.at(-2));
//reverse()
console.log('Sliced reversed',sliced.reverse());
//sort()
const numbers = [10,9,8,7,6,5,4,3,2,1];
console.log("Ascending order:",numbers.sort((a,b)=>a-b));
console.log("Descending order:",numbers.sort((a,b)=>b-a));
//map()
const mappedNumbers = numbers.map((number)=>{
return number + 10;
})
console.log("Mapped Numbers Array:",mappedNumbers);
//filter()
const evenNumbers = numbers.filter((number)=>{
return number%2 === 0;
})
console.log("Even Numbers Array:",evenNumbers);