-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.Some_more_Array_methods.js
37 lines (31 loc) · 1.04 KB
/
19.Some_more_Array_methods.js
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
// Delete function
let d = [3,51,22,41,262,38]
console.log(d);
delete d[0] // This will delete the first element from the array but space will be there
console.log(d,d[0]);
//Concat function to join two or more array
let d1 = [3,4,5,6,3,6]
let d2 = [3,6,3,7,5]
let a = d.concat(d1,d2)
console.log(a)
// Sort function
// sort function sort the array alphabetically so 1,12,2 like this
a.sort()
console.log(d);
//compare function for sort to sort by ascending order
let compare1= (a,b)=>{
return a-b
}
a.sort(compare1)
console.log(a)
// Reverse function reverse the array
a.reverse()
console.log(a)
// Splice function
let c =[1,2,3,4,5,6,7,8,9]
c.splice(2, 4 , 102)
// 2 means starting from index no 2, 4 means element to remove the 4 elements and 102 to replace the remove number
console.log(c)
// Slice function
console.log(c.slice(1)) // means it will slice from index no 1 to end // It doesnt effect the original array
console.log(c.slice(1,3)) // means it will slice from index no 1 to before of index no 3 // It doesnt effect the original array