-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray Loop.html
More file actions
117 lines (100 loc) · 3.36 KB
/
Array Loop.html
File metadata and controls
117 lines (100 loc) · 3.36 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>High Order Array Loop </title>
</head>
<body>
<script>
// ******** For of loop ***********
const arr=[1,2,3,4,5]
for (const num of arr) {
// console.log(num);
}
const greetings="Hello World"
for (const greet of greetings) {
if(greet === " ")
break;
// continue
// console.log(`Each char is:${greet}`);
}
// ******************** Map ************************
const map = new Map() //map is also a object in JS,hold the key value pair, also remeber a order of the keys,
map.set('PK' , "Pakistan")
map.set('UK' , "United Kingdom")
map.set('AF' , "Afghanistan")
map.set('PK' , "Pakistan") //Takes only a unique values
// console.log(map);
for (const[ key , value] of map) {
// console.log(key ,':-' , value);
}
// **************How to add a for of loop on a simple object
const myObject = {
game1 : 'NFS',
game2 : 'SpiderMan'
}
// for (const [ke , value] of myObject) { //this for of loop not work here
// console.log(key , ':-' , value);
// }
const myObject1 ={
js : "JavaScript",
cpp : "C++",
rb : "Ruby",
swift : "Swift for Apple"
}
for (const key in myObject1) {
// console.log(myObject1[key]);
// console.log(`${key} shortCut is for ${myObject1[key]}`);
}
const programing = ["js" , "rb" , "py" , 'java' , "cpp"]
for (const key in programing) {
// console.log(key); // the key of array is a numbers So we are using this
// console.log(programing[key]);
}
const map1 = new Map() //map is not itreate able so we can not use like this
map1.set('PK' , "Pakistan")
map1.set('UK' , "United Kingdom")
map1.set('AF' , "Afghanistan")
map1.set('PK' , "Pakistan") //Takes only a unique values
for (const[ key , value] in map1) {
// console.log(key ,':-' , value);
}
// ************************ For Each Loop ******************
const coding = ["js" , "rb" , "cpp" , 'py' ,'java']
// coding.forEach( function (item) {
// console.log(item);
// } ) //OR
// coding.forEach ( (val) =>{
// console.log(val);
// }) //OR
// function printMe (item) {
// console.log(item);
// }
// coding.forEach(printMe) //OR
// ****************** Also Prints ************
// coding.forEach((item , index , arr) => {
// console.log(item,index,arr);
// })
// ***************** Objects Inside Array how to access an object in arr ***************
const myCoding =[
{
langaugeName : "JavaScript",
langaugeFileName : "Js"
},
{
langaugeName : "Java",
langaugeFileName : "Java"
},
{
langaugeName : "Python",
langaugeFileName : "py"
},
]
myCoding.forEach ((item)=> { //Mostly work in a dataBase
// console.log(item.langaugeFileName);
// console.log(item.langaugeName);
});
</script>
</body>
</html>