-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-op.js
More file actions
126 lines (99 loc) · 2.29 KB
/
array-op.js
File metadata and controls
126 lines (99 loc) · 2.29 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
let arr1 = [1, 2, 3, 4, 5];
let arr2 = arr1.reverse();
let arr3 = [...arr1].reverse();
console.log(arr1);
console.log(arr2);
console.log(arr3);
let word = "ravi";
word.toUpperCase();
arr1.sort();
let word1 = word.toUpperCase();
console.log(word);
console.log(arr1);
console.log(word1);
let a = [1, 2, 3, 4, 5];
let b = [1, 2, 3, 5];
// compare two array
if (a.every((el, index) => el === b[index])) {
console.log("equal");
} else {
console.log("not equal");
}
//sort array
let alpha = ["t", "a", "c", "z"];
let ages = [22, 35, 15, 2, 6];
let students = [
{
name: "ravi",
age: 12,
},
{
name: "shreeraj",
age: 6,
},
{
name: "bhoite",
age: 60,
},
{
name: "poonam",
age: 36,
},
];
console.log(alpha.sort()); // working with letter
console.log(ages.sort()); // not working with numbers
console.log(ages.sort((a, b) => a - b));
console.log(students.sort((a, b) => a.age - b.age));
console.log(
students.sort((a, b) => (a.name == b.name ? 0 : a.name > b.name ? 1 : -1))
);
//array filter
console.log(ages.filter((el) => el % 2 === 0));
console.log(students.filter((stud) => stud.name.startsWith("b")));
console.log(students.filter((stud) => stud.age > 20));
// array min max element
console.log(Math.min(...ages));
console.log(Math.max(...ages));
let min = ages[0];
let max = ages[0];
for (let age of ages) {
if (age > max) {
max = age;
}
if (age < min) {
min = age;
}
}
console.log(min);
console.log(max);
let maxAge = students.reduce((acc, curr) => {
return acc.age > curr.age ? acc : curr;
});
console.log(maxAge);
// array unique element
let fruits = [
"apple",
"orange",
"banana",
"mango",
"orange",
"banana",
"cherry",
];
let uniqueFruits = [];
for (let fruit of fruits) {
if (!uniqueFruits.includes(fruit)) {
uniqueFruits.push(fruit);
}
}
console.log(uniqueFruits);
console.log([...new Set(fruits)]); // using sets
// empty array
arr1 = []; // does not work with const & reference copy of array
arr1.length = 0;
arr1.splice(0, arr1.length);
// last element of array
console.log(fruits[fruits.length - 1]);
console.log(fruits.slice(-1)[0]);
console.log(fruits.pop());
console.log(fruits.reverse()[0]);