-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopingThroughObjects.js
100 lines (84 loc) · 2.22 KB
/
loopingThroughObjects.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
94
95
96
97
98
99
100
const obj = {
name: "Samir",
id: 1,
country: "USA",
courses: ["English", "Math", "Higher Math"],
};
// Iterating over the object using different methods
function iterateObject(obj) {
// Using for...in loop
for (const key in obj) {
if (Object.hasOwnProperty.call(obj, key)) {
console.log(`${key} : ${obj[key]}`);
}
}
// Using Object.keys()
Object.keys(obj).forEach((key) => {
console.log(`${key} : ${obj[key]}`);
});
// Using Object.values()
Object.values(obj).forEach((value) => {
console.log(value);
});
// Using Object.entries()
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key} : ${value}`);
});
// Using for...of loop
for (const [key, value] of Object.entries(obj)) {
console.log(`${key} : ${value}`);
}
}
// Demonstrating map function
function demonstrateMap(obj) {
const arrOne = Object.keys(obj).map((key) => [key, obj[key]]);
console.log(arrOne);
const arrTwo = Object.values(obj).map((value) => typeof value === "string");
console.log(arrTwo);
const arrThree = Object.entries(obj).map(
([key, value]) => `${key}: ${value}`
);
console.log(arrThree);
}
// Demonstrating filter function
function demonstrateFilter(obj) {
const arrFour = Object.keys(obj).filter((key) => key !== "id");
console.log(arrFour);
const arrFive = Object.values(obj).filter(
(value) => typeof value !== "string"
);
console.log(arrFive);
const arrSix = Object.entries(obj)
.filter(([key, value]) => Array.isArray(value))
.flat();
console.log(arrSix);
}
// Demonstrating reduce function
function demonstrateReduce(obj) {
const arrSeven = Object.keys(obj).reduce((acc, key) => {
if (Array.isArray(obj[key])) {
return key;
}
return acc;
}, "");
console.log(arrSeven);
const arrEight = Object.values(obj).reduce((acc, value) => {
if (Number.isInteger(value)) {
return value;
}
return acc;
}, 0);
console.log(arrEight);
const arrNine = Object.entries(obj).reduce((acc, [key, value]) => {
if (key === "courses") {
acc[key] = value;
}
return acc;
}, {});
console.log(arrNine);
}
// Main execution
iterateObject(obj);
demonstrateMap(obj);
demonstrateFilter(obj);
demonstrateReduce(obj);