-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.html
116 lines (97 loc) · 3.46 KB
/
sample.html
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
<!-- Make sure to include the linq.js file for the sample to work -->
<html>
<head>
<script src="linq.js"></script>
<script type="text/javascript">
var cars = [
{'name': 'Camry', 'mpg': 20, 'color': 'red' },
{'name': 'Accord', 'mpg': 30, 'color': 'blue' },
{'name': 'Q7', 'mpg': 16, 'color': 'red' },
{'name': 'Passat', 'mpg': 15, 'color': 'black' },
{'name': 'CRV', 'mpg': 17, 'color': 'red' },
{'name': 'Mini', 'mpg': 24, 'color': 'red' },
{'name': 'Mustang', 'mpg': 16, 'color': 'white' },
{'name': 'Camaro', 'mpg': 15, 'color': 'red' },
{'name': 'Avalon', 'mpg': 24, 'color': 'red' }
];
//Cars with milage >= 20
function filter()
{
var result = ToEnumerable(cars).Where(function(x){
return x.mpg >= 20;}).ToList();
document.getElementById("result").innerText=JSON.stringify(result);
}
/*Output
[{"name":"Camry","mpg":20,"color":"red"},{"name":"Accord","mpg":30,"color":"blue"},{"name":"Mini","mpg":24,"color":"red"},{"name":"Avalon","mpg":24,"color":"red"}]
*/
//Transforms to another object
function transform()
{
var context = ToEnumerable(cars).Select(function(x){
return {car: x, status: 'In stock'};
}).ToList();
document.getElementById("result").innerText=JSON.stringify(context);
}
//Chaining ability. Chains the result.
function chain()
{
var chain = ToEnumerable(cars).Where(function(x){
return x.mpg >= 20;}).Select(function(x){
return {car: x, status: 'In stock'};
}).ToList();
document.getElementById("result").innerText=JSON.stringify(chain);
}
//Check if atleast object exists in an array with the condition satistied
//With out any parameter returns true is any item is present in the array
function exists()
{
var result = ToEnumerable(cars).Any(function(x){
return x.color == 'red';
});
document.getElementById("result").innerText=result;
}
//Check if all objects satisfy a condition in an array
function allExists()
{
var result = ToEnumerable(cars).All(function(x){
return x.color == 'red';
});
document.getElementById("result").innerText=JSON.stringify(result);
}
//Returns the first object that satisfies the condition on the object
//Without the parameter it returns the first object
function first()
{
var result = ToEnumerable(cars).First(function(x){
return x.color == 'blue';
});
document.getElementById("result").innerText=JSON.stringify(result);
}
//For loops sample. calls the calculate method for every object
function loop()
{
var results = [];
ToEnumerable(cars).ForEach(function(x){
results.push(calculate(x));
});
document.getElementById("result").innerText=JSON.stringify(results);
}
function calculate(car){
if(car.mpg > 20){
return true;
}
return false;
}
</script>
</head>
<body>
<input type='button' value='Filter cars with milage >=20' onclick='javascript:filter();'>
<input type='button' value='Transform to another object' onclick='javascript:transform();'>
<input type='button' value='Filter and then select(chaining)' onclick='javascript:chain();'>
<input type='button' value='Check if red color car exists' onclick='javascript:exists();'>
<input type='button' value='Check if all cars are red color' onclick='javascript: allExists();'>
<input type='button' value='return the first car with blue color' onclick='javascript: first();'>
<input type='button' value='For loop sample' onclick='javascript: loop();'>
<div id="result"></div>
</body>
</html>