- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
Latest commit
166 lines (114 loc) · 3.4 KB
/
Copy patharray.js
File metadata and controls
166 lines (114 loc) · 3.4 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
/*
creating array in java
int arr [] = {1,2};
int arr [] = new int[8];
*/
//creating array in java script
letarr=newArray(5);
arr[0]="Virat";
arr[1]="Root";
arr[2]="Babar";
arr[3]="Smith";
arr[4]="ken";
//getting all the element in the array
console.log(arr);
//first index
console.log(arr[0]);
//last index
console.log(arr[4])
console.log(arr.length-1);
//Another way of declaring array
//its can take mix data
letarr1=["a",1,3.5];
console.log(arr1);
letarr2=["Virat","Babar","Sakib","root"];
console.log(arr2);
//adding element from the back
arr2.push("Warner");
console.log(arr2);
//delete an element from the back
arr2.pop();
console.log(arr2);
//insert element from the front
arr2.unshift("Tamim");
console.log(arr2);
console.log(arr2[0]);
//delete element from the front
arr2.shift()
console.log(arr2);
//sorting an array in ascending order
console.log(arr2.sort());
//check the element present in an array
console.log(arr2.includes("Babar"))
//for loop
letarr3=["Virat","Babar","Sakib","root"];
for(consti=0;i<arr3.length;i++){
console.log(arr3[i])
}
letnumbers=[12,13,14,16,19];
letoddNumber=[];
for(leti=0;i<numbers.length;i++){
if(numbers[i]%2===1){
oddNumber.push(numbers[i]);
}
}
console.log(oddNumber)
//using of for each loop in js
//instead of : like java, we use in keyword in js
letarr4=["Virat","Babar","Sakib","root"];
for(letiinarr4){
console.log(arr4[i])
}
//nested array in js
letarr6=[['a','b'],[1,2]];
console.log(arr6)
//retrieving specific element from nested array
console.log(arr6[0][1]);
letarr7=[1,2,3,4,5,6,7,8];
letevenNumbers=[];
for(leti=0;i<arr7.length;i++){
if(arr7[i]%2==0){
evenNumbers.push(arr7[i])
}
}
console.log("Before using filter(): "+evenNumbers)
//use of filter(), map(), redeuce() in array
//create a new array with even number and multiply with 3 then sum it
/*use of filter() in array.
The filter() method is an iterative method. It calls a provided callbackFn function
once for each element in an array, and constructs a new array of all the values for which
callbackFn returns a truthy value. Array elements which do not pass the callbackFn test
are not included in the new array.
*/
//find the even numbers with filter()
letarr8=[1,2,3,4,5,6,7,8];
letfilterEvenNumbers2=arr8.filter((arr8)=>arr8%2==0);
console.log("After using filter(): "+filterEvenNumbers2)
/*
The map() creates a new array populated with the result of calling a provided function
on every element in the calling array.
*/
//create a new array with even number and multiply with 3 then sum it
//no () needed, if I have only parameter
//use of map()
letmappedArray=filterEvenNumbers2.map(arr8=>arr8*3)
console.log("After using map(): "+mappedArray)
/*
use of reduce();
The reduce() method is an iterative method. It runs a "reducer"
callback function over all elements in the array, in ascending-index order,
and accumulates them into a single value. Every time, the return
value of callbackFn is passed into callbackFn again on next invocation as accumulator .
*/
lettotalVal=mappedArray.reduce((sum,val)=>sum*val);
console.log("After using reduce(): "+totalVal)
//use of foreach()
//using one parameter
letarr9=[42,51,98,65,12];
arr9.forEach(v=>{
console.log(v)
})
//using 3 parameters with for each
arr9.forEach((v,i,a)=>{
console.log(v,i,a)
})