-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathArrayMethod2.js
More file actions
209 lines (83 loc) · 2.94 KB
/
ArrayMethod2.js
File metadata and controls
209 lines (83 loc) · 2.94 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Slice ??
// if we want to get the particular portion iof given array than we will use slice , this will not modify the original array
// it will to the shallow copy
const array = ["Cow" , "Cat" , "Dog" ,"Fox" , "Lion" , "Rat"];
const sliceArray = array.slice(); // if you will not pass any parameter that means it will copy all the array
// const slicePortion = array.slice(1 , 4); // if you will pass only one parameter this will be consider as the starting index
// // first parameter is starting index , second parameter is lastIndexc that is not include
// console.log(slicePortion) // (3) ['Cat', 'Dog', 'Fox']
// const slicePortion = array.slice(-5); // (5) ['Cat', 'Dog', 'Fox', 'Lion', 'Rat']
// console.log(slicePortion)
// const s = "Hello";
// console.log(s.slice())
// splice is use for removing and adding data from the array
// splice => prototype //
// Array.prototype.slice
const array2 = ["Cow" , "Cat" , "Dog" ,"Fox" , "Lion" , "Rat"];
array2.splice(0 ,2) // first index is starting point second argument is how many element yu want to delete
console.log(array2); [ "Dog" ,"Fox" , "Lion" , "Rat"]
// Adding the elements in array
array2.splice(0, 8 , "Cow" , "Cat" )
console.log(array2)
// Sorting in Js
const array3 = ["Cow" , "Cat" , "Dog" ,"Fox" , "Lion" , "Rat"];
array3.sort();
console.log(array3)
const array4 = [4000 , 6000 , 100 , 300 , 3];
const compareFunc = (a,b)=> {
return b-a
}
array4.sort(compareFunc)
array4.sort((a,b)=> a<b ? 1 : -1);
console.log(array4)
// FLATTEN ARRAY
// FLAT WRITE THE POLYFILL FOR FLAT
const arrray = [4,5, [87,8,8,[9,8,9,[8,9,9,[9,9,9,[9,8,9,[0,08]]]]]]];
console.log(arrray.flat(Infinity));
// OUTPUT // [3,4,6,7,7,8,8]
function flattenArr(arr, res) {
for (const ele in arr) {
if(Array.isArray(ele)){
flattenArr(ele, res);
}
else{
res.push(arr[ele]);
}
}
return res;
}
const testArray = [23 ,45 ,67 , 89 , 100 , 5 , 10 , 20];
// 5 minutes => 10 minutes
// const result = {
// '100': '100 is even' ,
// '10': '10 is Even',
// '20': '20 is even'
// }
// [{'100': '100 is even'} ,{'10': '10 is Even'} , {'20': '20 is even'}]
const modifyObject=()=> {
const result = {};
testArray.forEach((item , index)=>{
if(item%2==0) {
result[item] = `${item} is even`
}
})
return result;
}
console.log(modifyObject(testArray))
const modifiedArray = testArray.filter((item)=>{
return item%2==0;
}).map((item)=>{
return {
item: `${item} is even`
}
})
console.log(modifiedArray)
// const shallowCopy = array;
// console.log(shallowCopy)
// shallowCopy[0] =300;
// console.log(array)
// // deep which is also called clone deep
// const cloneDeep = [...array] // spread operator is also used for the single levele object clone deep
// console.log(cloneDeep)
// cloneDeep[0]= "Hey"
// console.log(cloneDeep)