-
Notifications
You must be signed in to change notification settings - Fork 0
/
json for loops iteration.js
48 lines (37 loc) · 1.11 KB
/
json for loops iteration.js
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
var person = { "name": "robin", "age": "24", "location": "India" }
for (var key in person) { //using for loop
console.log(key + ": " + person[key])
}
var keys = Object.keys(person) //generating keys array for JSON
for (let key of keys) { //using for of loop
console.log(key + ": " + person[key])
}
keys.forEach(key => console.log(key + ": " + person[key])) //using for each loop
for (var i = 0; i < keys.length; i++)
console.log(keys[i] + ": " + person[keys[i]]) //using for loop
//successfully iterated through all loops.
//json objects enclosed in array
var json = [{
"id": "1",
"msg": "hi",
"time": "2021-05-05 23:35",
"fromWho": "hello1@gmail.com"
},
{
"id": "2",
"msg": "there",
"time": "2021-05-05 23:45",
"fromWho": "hello2@gmail.com"
}];
//for
for (var i = 0; i < json.length; i++) {
console.log(json[i]);
}
//forEach
json.forEach(obj => console.log(obj))
//for in
for (var ind in json)
console.log(json[ind])
//for of
for (var val of json)
console.log(val)