-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-Fetch.js
171 lines (113 loc) · 3.59 KB
/
5-Fetch.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* ------------------------------------------------------ */
// 1. JSON => stringify, parse
// #. JSON => (JavaScript Object Notation)
const student = {
name: 'Saki Khan',
age: 32,
movies: ['king khan', 'Dhaka Mast']
}
// JSON.stringify --------------->!!!
const studentJSON = JSON.stringify(student);
// console.log(student);
// console.log(studentJSON);
// JSON.parse --------------->!!!
const studentObj = JSON.parse(studentJSON);
// console.log(studentObj);
/* ------------------------------------------------------ */
//2. fetch
// No-1
fetch('url')
.then(res => res.json())
.then(data => console.log(data));
// No-2
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
// No-3
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
}
}
fetchData();
// No-3.1
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
}
}
fetchData();
/* ------------------------------------------------------ */
// 3. Object => keys, values
const keys = Object.keys(student);
const values = Object.values(student);
/* ------------------------------------------------------ */
// 4. Array => for loop
const numbers = [23, 54, 67, 87, 23, 78];
// Using a for loop
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
// No-1 ----------!!!
// Using forEach
numbers.forEach(num => console.log(num));
// No-2 ----------!!!
// Using map() to double each number
const doubledNumbers = numbers.map(num => num * 2);
console.log(doubledNumbers);
// No-3 ----------!!!
// for of
const fruits = ["apple", "banana", "cherry"];
for (const fruit of fruits) {
console.log(fruit);
}
// No-3.1 ----------!!!
// for of => on array like object.
const fruitsObj = {
0: "apple",
1: "banana",
2: "cherry",
length: 3,
};
for (const fruit of Object.values(fruitsObj)) {
console.log(fruit);
}
// No-4 ----------!!!
// for in => loop on an object keys (property) using for in.
const person = {
name: "John",
age: 30,
job: "Engineer"
};
for (const key in person) {
console.log(key);
}
/* ------------------------------------------------------ */
// # Spread Operator
// add or remove from an array using (...spread operator) and (filter method).
const products = [
{ name: 'laptop', price: 3200, brand: 'len', color: 'silver' },
{ name: 'phone', price: 7000, brand: 'HTC', color: 'golden' },
{ name: 'watch', price: 3000, brand: 'casio', color: 'yellow' },
{ name: 'sunglass', price: 300, brand: 'ray', color: 'black' },
{ name: 'camera', price: 9000, brand: 'canon', color: 'gray' }
];
const newProduct = { name: 'webcam', price: 3500, color: 'black' };
// Copy products array and then add newProduct using spread operator
const newProducts = [...products, newProduct];
/* ------------------------------------------------------*/
// # filter /* only condition Return array */
// Create a new array without one specific item. Remove phone(item) means create a new array without the phone
const remaining = products.filter(products => products.name !== 'phone');