-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL4.js
More file actions
171 lines (139 loc) · 4.24 KB
/
L4.js
File metadata and controls
171 lines (139 loc) · 4.24 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
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
// Array of objects
function Product(name, price) {
this.name = name;
this.price = price;
}
var data = [
new Product("Coffee", 2.5),
new Product("Tea", 2.5),
new Product("Milk", 2.5),
new Product("Cake", 10),
new Product("Pie", 5),
];
var data2 = data;
// The reference of the array get copied
// if we change the data2, it will also change the data
data2[0].name = "Coffee2";
// console.log(data[0].name);
data.forEach(
function (item, index) {
console.log(item.name + " " + item.price);
}
);
// Dont use forEach if you will be manupulating the array
var changedData = data.map(
function (item, index) {
item.tax = item.price * 0.2;
return item;
}
);
console.log(changedData);
// Map apply the function to each item in the array and return the new array
var productNames = data.map(
function (item, index) {
return item.name;
}
);
console.log(productNames);
console.log(productNames.join(", ")); // Join the array to a string
var filteredData = data.filter(
function (item, index) {
return item.price > 3; //return a boolean value
}
);
console.log(filteredData);
var filteredData2 = data.filter(
function (item, index) {
return item.price > 3;
}
).map(
function (item, index) {
return new Product(item.name, item.price);
// return { //both are same thing execpt if product constructer have other properties/methods then it will not be there
// name: item.name,
// price: item.price
// }
}
);
filteredData2[0].name = "Coffee3";
console.log(filteredData2);
console.log(data);
var findedData = data.find(
function (item, index) {
return item.name === "Cake";
}
);
console.log(findedData);
// Find the first item that matches the condition
var findedIndex = data.findIndex(
function (item, index) {
return item.name === "Cake";
}
);
console.log(findedIndex);
// Find the index of the first item that matches the condition
// If not found, it will return -1
// Sort the array
var sortedData = data.sort(
function (item1, item2) {
// return item1.price - item2.price; //ascending
return item2.price - item1.price; // descending order
}
);
console.log(sortedData);
// Swap the item if the return value is negative
// If the return value is positive, it will keep the original order
var slicedData = data.slice(0, 2); // return the first 2 items
console.log(slicedData);
// Slice the array starting from first argument and ending at second argument - 1
var splittedData = data.splice(0, 2); // return the first 2 items and remove them from the array
console.log(splittedData);
console.log(data);
// the difference between splice and slice is that splice will remove the items from the array
// var newData = [data.splice(0, 2), data.splice(0, 2)]; // this will create a new array with 2 arrays
// console.log(newData);
var newData = data.splice(1, 2).concat(data.splice(0, 2)); // this will create a new array with 2 arrays
console.log(newData);
// Serialization and Deserialization
var obj = {
name: "John",
age: 30,
address: {
street: "123 Main St",
city: "New York",
state: "NY"
},
getInfo : function () { // function is not serializable -> only data
return this.name + " " + this.age;
}
};
var objJSON = JSON.stringify(obj);
console.log(objJSON);
// Serialize the object to a string
// When object is converted to string, it is called json string
// Deserialize the string to an object
// console.log(JSON.parse(objJSON));
// Recomended to use try catch block to handle the error
try {
var obj2 = JSON.parse(objJSON);
console.log(obj2);
}
catch (error) {
console.log(error);
}
function Product(name, price) {
this.name = name;
this.price = price;
}
Product.prototype.type = "Base Product"; // this will not be serialized
var p1 = new Product("Coffee", 2.5);
var p1JSON = JSON.stringify(p1);
console.log(p1JSON);
var p1Copy = JSON.parse(p1JSON);
console.log(p1Copy);
// A lot of things get lost when you serialize an object
// You can also use these function on arrays
// ECMAScript
// JavaScript follows the ECMAScript standard
// https://en.wikipedia.org/wiki/ECMAScript
// New features in ECMAScript 6 -> ES6