-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.js
93 lines (64 loc) · 1.75 KB
/
example.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
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
// for loop
const arr =["Ahmet", "Mehmet", "Selin"];
for(let i=0; i<arr.length; i++){
console.log(i, "--", arr[i]);
}
// for .. in loop
const arr2 =["Comment1", "Comment2", "Comment3"];
for(let prop in arr2){
console.log(prop, "--", arr2[prop]);
}
// for .. of loop
const langs =["Java", "C#", "C++","PHP"];
for(let lang of langs){
console.log(lang);
}
// foreach
const arr3 =["Comment1", "Comment2", "Comment3"];
arr3.forEach(function(value,key){
console.log(value, "--", key);
})
// map
const arr4 =["Comment1", "Comment2", "Comment3"];
arr4.map(function(c){
console.log(c);
});
// for in with object
const person = {
name:"Serdar",
surname:"ALKAN",
job:"Software Developer",
department:"IT"
};
for(let key in person){
if(person.hasOwnProperty(key)){
console.log(key, "--", person[key]);
}
}
// for of with object => Not traverse -> ERROR
// for(let prop of person){ -> ERROR
// console.log(prop);
// }
for(let key of Object.keys(person)){ // for .. of -> NO ERROR
console.log(key, "--", person[key]);
};
// foreach with object => Not traverse -> ERROR
// person.forEach(function(value,key){ -> ERROR
// console.log(value, "--", key);
// })
Object.keys(person).forEach(function(key){ // foreach -> NO ERROR
console.log(key, "--", person[key]);
});
Object.entries(person).forEach(entry => { // foreach -> NO ERROR
console.log(entry[0], "--", entry[1]);
});
Object.values(person).forEach(value => { // foreach -> NO ERROR
console.log(value);
});
Object.getOwnPropertyNames(person).forEach( key => { // foreach -> NO ERROR
console.log(key, "--", person[key]);
});
// map with object traverse -> ERROR
// person.map(function(lang){ -> ERROR
// console.log(lang);
// });