-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
83 lines (66 loc) · 1.59 KB
/
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
// 1.JSON => stringify and parse
const student = {
name: 'jerin',
id: 38,
age: 23,
movies: ['x', 'y'],
}
const studentJSON = JSON.stringify(student);
console.log(student);
console.log(studentJSON);
const studentObj = JSON.parse(studentJSON);
console.log(studentObj);
// 2. Fetch
fetch('url')
.then(response => response.json())
.then(data => console.log(data));
// keys, values
const keys = Object.keys(student);
console.log(keys);
const values = Object.values(student);
console.log(values);
// for
const numbers = [23, 54, 67, 97, 111];
numbers.forEach(num => console.log(num));
numbers.map(num => num * 2);
// for of array like object
// loop on an object using for in
// add or remove for an array
const products = [
{
name: 'laptop',
price: 32000,
brand: 'lenovo',
color: 'silver',
},
{
name: 'phone',
price: 7000,
brand: 'iphone',
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: 700, brand: 'lal' }
// copy products array and then Add newProducts
const newProducts = [...products, newProduct];
// create a new array without one specific item
// remove phone means create a new array without the phone
const remaining = product.filter(product => product.name !== 'phone');