-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArray_Functions_2.html
More file actions
132 lines (98 loc) · 4.32 KB
/
Array_Functions_2.html
File metadata and controls
132 lines (98 loc) · 4.32 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
<html>
<head>
<script type="text/javascript">
//--------------------------------------------------------------------------------------------------
// #LOOP : Function to get the details of students who got more than 80 percent
function fnFilterStudents_loop(aStudent){
var tempArr = [];
for(var i = 0 ; i < aStudent.length; i ++){
if(aStudent[i].fPercentage > 80.0) { tempArr.push(aStudent[i]);}
}
return tempArr;
}
aStudent = [
{ sStudentId : "001" , fPercentage : 91.2 },
{ sStudentId : "002" , fPercentage : 78.7 },
{ sStudentId : "003" , fPercentage : 62.9 },
{ sStudentId : "004" , fPercentage : 81.4 }];
console.log(fnFilterStudents_loop(aStudent));
//Output: [{ sStudentId : "001" , fPercentage : 91.2 },
// { sStudentId : "004" , fPercentage : 81.4 }];
//--------------------------------------------------------------------------------------------------
// #FILER : Function to get the details of students who got more than 80 percent
function fnFilterStudents_filter(aStudent){
return aStudent.filter(function(oStudent){
return oStudent.fPercentage > 80.0 ? true : false;
});
}
aStudent = [
{ sStudentId : "001" , fPercentage : 91.2 },
{ sStudentId : "002" , fPercentage : 78.7 },
{ sStudentId : "003" , fPercentage : 62.9 },
{ sStudentId : "004" , fPercentage : 81.4 }];
console.log(fnFilterStudents_filter(aStudent));
//Output: [{ sStudentId : "001" , fPercentage : 91.2 },
// { sStudentId : "004" , fPercentage : 81.4 }];
//--------------------------------------------------------------------------------------------------
// Function to remove undefined elements from array
function removeUndefined(myArray){
return myArray.filter(function(element, index, array){
return element;
});
}
var arr = [1,undefined,3,undefined,5];
console.log(arr);
// Output : [1, undefined, 3, undefined, 5]
console.log(removeUndefined(arr));
// Output : [1, 3, 5]
//--------------------------------------------------------------------------------------------------
// #lOOP : Function to add property bIsDistinction to each element
function fnAddDistinction_loop(aStudent){
for(var i = 0 ; i < aStudent.length ; i ++){
aStudent[i].bIsDistinction = (aStudent[i].fPercentage >= 75.0) ? true : false;
}
return aStudent;
}
aStudent = [
{ sStudentId : "001" , fPercentage : 91.2 },
{ sStudentId : "002" , fPercentage : 78.7 },
{ sStudentId : "003" , fPercentage : 62.9 },
{ sStudentId : "004" , fPercentage : 81.4 }
];
console.log(fnAddDistinction_loop(aStudent));
//output: [{ sStudentId : "001" , fPercentage : 91.2 , bIsDistinction : true },
// { sStudentId : "002" , fPercentage : 78.7 , bIsDistinction : false },
// { sStudentId : "003" , fPercentage : 62.9 , bIsDistinction : false },
// { sStudentId : "004" , fPercentage : 81.4 , bIsDistinction : true }];
//-------------------------------------------------------------------------------------------------
// #MAP : Function to add property bIsDistinction to each element
function fnAddDistinction_map(aStudent){
return aStudent.map(function(student, index, array){
student.bIsDistinction = (student.fPercentage >= 75.0) ? true : false;
return student;
});
}
aStudent = [
{ sStudentId : "001" , fPercentage : 91.2 },
{ sStudentId : "002" , fPercentage : 78.7 },
{ sStudentId : "003" , fPercentage : 62.9 },
{ sStudentId : "004" , fPercentage : 81.4 }
];
console.log(fnAddDistinction_map(aStudent));
//output: [{ sStudentId : "001" , fPercentage : 91.2 , bIsDistinction : true },
// { sStudentId : "002" , fPercentage : 78.7 , bIsDistinction : false },
// { sStudentId : "003" , fPercentage : 62.9 , bIsDistinction : false },
// { sStudentId : "004" , fPercentage : 81.4 , bIsDistinction : true }];
//--------------------------------------------------------------------------------------------------
[1,4,9].map(Math.sqrt);
// Output : [1,2,3]
["1.232","9.345","3.2345"].map(parseFloat);
// Output : [1.232, 9.345, 3.2345]
["1","2","3"].map(parseInt);
// Output: [1, NaN, NaN]
["1","2","3"].map(function(val){return parseInt(val,10)});
//Output: [1, 2, 3]
</script>
</head>
<body></body>
</html>