-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusingforofloopswithobjects.js
More file actions
55 lines (43 loc) · 1.33 KB
/
usingforofloopswithobjects.js
File metadata and controls
55 lines (43 loc) · 1.33 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
// JavaScript how to use the for of loop with objects
// object with its properties
const skater = {
name: 'JRAD',
stance: 'regular',
fractures: 5,
bonesBroken: 0,
ankleInjuries: 57,
};
//we get a type error because skater is not iterable
// for(const prop of skater) {
// console.log(prop);
// }
//try this
// an object is not an array like and this is not a function
// for(const prop of skater.enteries()) {
// console.log(prop);
// };
// we got the keys in the object
// for(const prop of Object.keys(skater)) {
// console.log(prop);
// };
// to get the values you
// sketchy because of the for of loop that is reaching the skater object outside
// for(const prop of Object.keys(skater)) {
// console.log(prop, skater[prop]);
// };
// ES2017 two methods released for object constructor
// this lays out the values alone
// for(const prop of Object.values(skater)) {
// console.log(prop);
// };
// 2nd method
// entries is being called on object constructor entries is not going to be a
// method on the prototype of the object have to call it from the object constructor method
// for(const prop of Object.enteries(skater)) {
// console.log(prop);
// };
// we can destructor
// then makes the object have the ability of being iterable
for(const [prop, value] of Object.enteries(skater)) {
console.log(prop, value);
};