-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.js
More file actions
223 lines (173 loc) · 5.07 KB
/
json.js
File metadata and controls
223 lines (173 loc) · 5.07 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var json = {
//Number
number : 1,
decimal: 1.2,
//String
string: 'this is string',
//Types of arrays
//empty array
emptyArray : [],
//Array of Numbers
arrayOfNumbers: [1, 2, 3, 4, 5],
//array of string
arrayOfStrings: ['one', 'two', 'three', 'four'],
//2-D array
//Array of Arrays
arrayOfArrays: [
[1, 2, 3, 4, 5],
[11, 12, 13, 14, 15],
[21, 22, 23, 24, 25],
[31, 32, 33, 34, 35]
],
//3-D array
'3DArray' : [
[
[1, 2, 3, 4, 5]
],
[
[6, 7, 8, 9, 10]
]
],
//Array of Objects
arrayOfObjects: [
{
userId: 1,
username: 'hbarve1'
},
{
userId: 2,
username: 'yukta'
},
{
userId: 3,
username: 'mayash'
}
],
//Simple Function
func: function() {
console.log('This is a function');
},
//Array of functions.
arrayOfFunction: [
function() {
console.log('This is an array function one');
return 'This is an return string from an array function one.';
},
function() {
console.log('This is an array function two');
}
],
//Object of Functions.
objectOfFunctions: {
funcOne: function() {
console.log('This is a first function of objectOfFunctions');
},
funcTwo: function() {
console.log('This is a second function of objectOfFunctions');
}
}
};
// console.log(json.number);
// console.log(json.string);
// console.log(json.emptyArray);
// console.log(json.arrayOfNumbers);
// console.log(json.arrayOfStrings);
// console.log(json.arrayOfArrays);
// console.log(json['3DArray']);
// console.log(json.arrayOfObjects);
// console.log(json.arrayOfFunction);
// console.log(json.objectOfFunctions);
/*output of above code
1
this is string
[]
[ 1, 2, 3, 4, 5 ]
[ 'one', 'two', 'three', 'four' ]
[ [ 1, 2, 3, 4, 5 ],
[ 11, 12, 13, 14, 15 ],
[ 21, 22, 23, 24, 25 ],
[ 31, 32, 33, 34, 35 ] ]
[ [ [ 1, 2, 3, 4, 5 ] ], [ [ 6, 7, 8, 9, 10 ] ] ]
[ { userId: 1, username: 'hbarve1' },
{ userId: 2, username: 'yukta' },
{ userId: 3, username: 'mayash' } ]
[ [Function], [Function] ]
{ funcOne: [Function], funcTwo: [Function] }
*/
//---------------------------------------------------------------------------
//One method of calling all values of an array.
var i = 0;
/*
defining an object "arrayOfNumbers" which contain value of Object "JSON"-key"arrayOfNumbers"
NOTE: it is only for convenience so we have not to access value again.
*/
var arrayOfNumbers = json.arrayOfNumbers;
// for(i = 0; i < arrayOfNumbers.length; i++) {
// console.log(arrayOfNumbers[i]);
// }
//-----------------------------------------------------------------------------------
//Second method of calling all values of an array.
// function numberInArray(value, index) {
// console.log(value, index);
// }
//[].forEach() is an in-build method.-- which work same as "for" loop.
//[].forEach() will call a function for every value in array.
// arrayOfNumbers.forEach(numberInArray);
// var arrayOfStrings = json.arrayOfStrings;
// /*Here I have defined new function inside an [].forEach() method.
// It will work same as for loop*/
// arrayOfStrings.forEach(function stringOfArray(str, index) {
// console.log(index, str);
// });
//
//-----------------------------------------------------------------------------------------
/*Printing 2D-array*/
var arrayOfArrays = json.arrayOfArrays;
// var j = 0;
// var k = 0;
// for(j = 0; j < arrayOfArrays.length; j++) {
// for(k = 0; k < arrayOfArrays[j].length; k++) {
// console.log('arrayOfArrays[' + j + '][' + k + '] = ' + arrayOfArrays[j][k]);
// }
// }
//[].map() and [].forEach() are same.
/*using nested map is same as printing 2D-array via "for" loop
given below*/
// arrayOfArrays.map(function (array, index1) {
// console.log(index1, array);
// array.map(function(value, index2) {
// console.log('arrayOfArrays[' + index1 + '][' + index2 + '] = ' + value);
// });
// });
//---------------------------------------------------------------------------------------------
// var arrayOfObjects = json.arrayOfObjects;
// arrayOfObjects.map(function (obj, index) {
// console.log(index, obj, obj.userId, obj.username);
// });
/*output for above
0 { userId: 1, username: 'hbarve1' } 1 'hbarve1'
1 { userId: 2, username: 'yukta' } 2 'yukta'
2 { userId: 3, username: 'mayash' } 3 'mayash'
*/
//-----------------------------------------------------------------------------------------------
//For calling a function which is inside a JSON
// json.func();
//
// /*other way to call function*/
// var temp = json.func();
// temp;
//----------------------------------------------------------------------------------------------
//Array of functions
var arrayOffunctions = json.arrayOfFunction;
//First method.
arrayOffunctions[0]();
arrayOffunctions[1]();
//Second method
/*it will also use return values*/
arrayOffunctions.map(function(func, index) {
console.log(index, func());
});
//-----------------------------------------------------------------------------------------------
//calling objectoffunctions
json.objectOfFunctions.funcOne();
json.objectOfFunctions.funcTwo();