Repository navigation
Expand file tree
/
Copy path09_arrays_methods.js
More file actions
126 lines (72 loc) · 2.82 KB
/
Copy path09_arrays_methods.js
File metadata and controls
126 lines (72 loc) · 2.82 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//15. forEach() - execute a function for every element
number.forEach((n) => {
console.log("Number :", n);
});
//16. includes() - check whether an element exists
console.log("Is 4 present?", number.includes(4));
console.log("Is 10 present?", number.includes(10));
//17. indexOf() - find the first index of an element
console.log("Index of 3 :", number.indexOf(3));
//18. lastIndexOf() - find the last index of an element
let nums = [1, 2, 3, 2, 4, 2];
console.log("Last index of 2 :", nums.lastIndexOf(2));
//19. findLast() - find the last element satisfying a condition
let lastEven = number.findLast(n => n % 2 === 0);
console.log("Last even number :", lastEven);
//20. findLastIndex() - find the index of the last matching element
let lastEvenIndex = number.findLastIndex(n => n % 2 === 0);
console.log("Last even index :", lastEvenIndex);
//21. flatMap() - map + flat together
let values = [1, 2, 3];
let result = values.flatMap(n => [n, n * 2]);
console.log("flatMap :", result);
// [1, 2, 2, 4, 3, 6]
//22. Array.from() - create an array from an iterable/string
let word = "HELLO";
let letters = Array.from(word);
console.log("Array from string :", letters);
//23. Array.of() - create an array from given values
let data = Array.of(10, 20, 30, 40);
console.log("Array.of :", data);
//24. fill() - replace elements with a value
let fillArray = [1, 2, 3, 4, 5];
fillArray.fill(0, 1, 4);
console.log("After fill :", fillArray);
// [1, 0, 0, 0, 5]
//25. copyWithin() - copy part of an array to another position
let copyArray = [1, 2, 3, 4, 5];
copyArray.copyWithin(0, 3);
console.log("After copyWithin :", copyArray);
// [4, 5, 3, 4, 5]
//26. toSorted() - sort without changing original array
let original = [5, 2, 8, 1];
let sorted = original.toSorted((a, b) => a - b);
console.log("Original :", original);
console.log("Sorted :", sorted);
//27. toReversed() - reverse without changing original array
let original2 = [1, 2, 3, 4];
let reversed = original2.toReversed();
console.log("Original :", original2);
console.log("Reversed :", reversed);
//28. toSpliced() - splice without changing original array
let original3 = [1, 2, 3, 4, 5];
let newArray = original3.toSpliced(2, 1, 99);
console.log("Original :", original3);
console.log("New array :", newArray);
//29. at() - access element using positive or negative index
let colors = ["red", "green", "blue", "yellow"];
console.log("First :", colors.at(0));
console.log("Last :", colors.at(-1));
//30. entries() - get index and value together
let fruits2 = ["mango", "apple", "banana"];
for (let [index, value] of fruits2.entries()) {
console.log(index, value);
}
//31. keys() - get array indexes
for (let index of fruits2.keys()) {
console.log("Index :", index);
}
//32. values() - get array values
for (let value of fruits2.values()) {
console.log("Value :", value);
}