-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforOfLoop.js
More file actions
120 lines (98 loc) · 3.22 KB
/
forOfLoop.js
File metadata and controls
120 lines (98 loc) · 3.22 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
//t's important to know that a for of loop cannot work on an object directly, since an object is not iterable.
const car = {
speed: 100,
color: "blue"
}
for(prop of car) {
console.log(prop)
}
//Running the above code will throw an error
//Contrary to objects, arrays are iterable. For example:
const colors = ['red','orange','yellow']
for (var color of colors) {
console.log(color);
}
//The output of the above code is as follows: red, oranage, yellow
//The for of loop can be run on arrays and also can be used to loop over objects. To do this, you need to use the Object.keys() method, which receives an object as its parameter. Remember, this object is the object you want to loop over. An array of properties is returned when you call the Object.keys() method as shown:
const car2 = {
speed: 200,
color: "red"
}
console.log(Object.keys(car2)); // ['speed','color']
//Object.values() returns an array of values contained in the object
const car3 = {
speed: 300,
color: "yellow"
}
console.log(Object.values(car3)); // [300, 'yellow']
//The Object.entries() returns an array listing both the keys and the values.
const car4 = {
speed: 400,
color: 'magenta'
}
console.log(Object.entries(car4)); //[ ['speed', 400], ['color', 'magenta'] ]
//The values that get returned are 2-member arrays nested inside an array. In other words, you get an array of arrays, where each array item has two members, the first being a property's key, and the second being a property's value.
// Effectively, it's as if you was listing all of a given object's properties, a bit like this:
// [
// [propertyKey, propertyVal],
// [propertyKey, propertyVal],
// ...etc
// ]
var clothingItem = {
price: 50,
color: 'beige',
material: 'cotton',
season: 'autumn'
}
for( key of Object.keys(clothingItem) ) {
console.log(keys, ":", clothingItem[key])
}
//The for of loop only iterates over the object's own properties and does not count the prototype at all. While the for in loop iterates over the properties and not the prototype
//Template literals
let greet = "Hello";
let place = "World";
console.log(`${greet} ${place} !`) //display both variables using template literals
//The above code returns: Hello World !
//Using ES5 methods, you can only create strings using single or double quotations and using this method does not support the use of multi-line strings. In ES6, it is possible using the following syntax:
var multiLineString=`
This
is
a
multiline
string
made possible
with bac-ticks in
ES6
`
console.log(multiLineString);
// Task 1
var dairy = ['cheese', 'sour cream', 'milk', 'yogurt', 'ice cream', 'milkshake']
function logDairy(){
for(var item of dairy){
console.log(item)
}
}
// Task 2
const animal = {
canJump: true
};
const bird = Object.create(animal);
bird.canFly = true;
bird.hasFeathers = true;
function birdCan() {
for (var prop of Object.keys(bird)) {
console.log(prop + ": " + bird[prop])
}
}
// Task 3
function animalCan() {
for (var prop of Object.keys(bird)) {
console.log(prop + ": " + bird[prop])
}
for (var prop of Object.keys(animal)) {
console.log(prop + ": " + animal[prop])
}
}
logDairy();
birdCan();
animalCan();