-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArray_Functions_3.html
More file actions
110 lines (66 loc) · 2.47 KB
/
Array_Functions_3.html
File metadata and controls
110 lines (66 loc) · 2.47 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
<html>
<body>
<script>
function reduceFun1(previousValue, currentValue, index, array){
return previousValue + currentValue;
}
var result = [1,2,3,4,5].reduce(reduceFun1);
console.log(result); // Output : 15
var arr=[{name: "customer 1", sales: 500, location: "NY"},
{name: "customer 1", sales: 200, location: "NJ"},
{name: "customer 1", sales: 700, location: "NY"},
{name: "customer 1", sales: 200, location: "ORD"},
{name: "customer 1", sales: 300, location: "NY"}];
function reduceFun2(previousValue, currentValue, index, array){
if(currentValue.location === "NY"){
return previousValue + currentValue.sales;
}
return previousValue;
}
var totalSalesInNY = arr.reduce(reduceFun2);
console.log(totalSalesInNY); // Output: 1500
var arr1 = [1,2,3,4];
var arr2 = [5,6,7,8];
var arr3 = arr1.concat(arr2);
console.log(arr3); // Output : [1, 2, 3, 4, 5, 6, 7, 8]
var arr1 = [1,2,3,4];
var arr2 = [5,6];
var arr3 = [7,8];
var arr4 = arr1.concat(arr2,arr3);
console.log(arr4); // Output : [1, 2, 3, 4, 5, 6, 7, 8]
var arr1 = [1,2,3,4];
var arr2 = arr1.concat(5,6,7,8);
console.log(arr2); // Output : [1, 2, 3, 4, 5, 6, 7, 8]
var arr1 = [1,2,3,4];
var arr2 = [5,6];
var arr3 = arr1.concat(arr2, 7, 8);
console.log(arr3); // Output : [1, 2, 3, 4, 5, 6, 7, 8]
var arr = [3, 1, 5, 4, 2].sort();
console.log(arr); // Output : [1, 2, 3, 4, 5]
var arr = ["Orange", "Apple", "Banana"].sort();
console.log(arr); // Output : ["Apple", "Banana", "Orange"]
var arr = [1, 30, 5].sort();
console.log(arr); // Output : [1, 30, 5]
function sortFun(a,b){
return a - b;
}
var arr = [1, 30, 5].sort(sortFun);
console.log(arr); // Output : [1, 5, 30]
var arr = [{name:"NY", count: 20},
{name:"NJ", count: 5},
{name:"JFK", count: 60},
{name:"ORD", count: 1}];
function sortFun(obj1, obj2){
return obj1.count - obj2.count;
}
arr.sort(sortFun);
console.log(arr); // Output [{name:"ORD", count: 1},
// {name:"NJ", count: 5},
// {name:"NY", count: 20},
// {name:"JFK", count: 60}]
var arr = [1,2,3,4,5,6,7,8,9,10];
arr.reverse();
console.log(arr); //Output : [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
</script>
</body>
</html>