-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrev4.js
More file actions
198 lines (154 loc) · 3.64 KB
/
rev4.js
File metadata and controls
198 lines (154 loc) · 3.64 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//for each
let names=['alice','bob','peter','jane'];
names.forEach((name)=>{
console.log(name+" ji");
})
//map
let newnames=names.map((name)=>{
return name;
})
console.log(newnames);
//filter
let condnames=names.filter((name)=>{
return name==='alice'
})
console.log(condnames)
//destructuring
let obj={
name:"marry",
age:[20,30,40],
address:{
karachi:"gulshan",
lahore:"laal qila"
},
country:"Pakistan"
}
let access1=obj.age.at(0)
let access2=obj.address.karachi
console.log(access1);
console.log(access2);
let {karachi,lahore} =obj.address
console.log(lahore);
let [f,s] =obj.age
console.log(s)
//spread
//premetives pass by value
let a=4;
let b=a;
console.log(b+1);
console.log(a);
//nonpremitives pass by ref
let array1=[1,2,3]
// let array2=array1
// array2.pop()
// console.log(array2);
// console.log(array1);
//to solve this prob we use spread op
let array2=[...array1];
array2.pop()
console.log(array2);
console.log(array1);
//same for object
let obj1={
name:"hello",
color:"black"
}
let obj2={...obj1}
obj2.name="mak";
console.log(obj1)
console.log(obj2)
//spread op is only level one operator (que asked in interview)
let list={
add:{
name:"mak"
},
tag:"sad"
}
let list2={...list}
list2.add.name="fak";
console.log(list)
console.log(list2)
//rest op
//bajay ye k ap multiple var banay in fun par we can store val in an array code optimization and memorysaves wrt space
let bilal =function(...val){
console.log(val)
}
bilal(1,2,3,4);
//que 1
//concat two arrays fetched from two diff apis
//concat is just mix or paste two arrays into one
//interview que
let concat1=["raima","maria","andleeb"];
let concat2=["ali","adnan","mubashir"];
let students=[...concat1,...concat2]
console.log(students);
//if i have to merge then I should have a logic
let topstudents=concat1.concat(concat2).sort()//logic is sorting according to alphabetical order
console.log(topstudents)
//Custom Sorting with Comparator Function:
let num1=[3,4,1,3,70]
let sortednum1=num1.sort((a,b)=>a-b)
console.log(sortednum1)
let num2=[20,60,5,3]
let arr=num1.concat(num2).sort((a,b)=>a-b);
console.log(arr);
let arr1=[...num1,...num2].sort((a,b)=>a-b)
console.log(arr1);
//que2
//filter an array of obj to display item based on search query eg filter prod by type
let products=[
{name:"phone",type:"electronic"},
{name:"pen",type:"stationary"},
{name:"computer",type:"electronic"},
{name:"hairbrush",type:"beauty"},
{name:"jacket",type:"clothing"}
]
let search =products.filter((product)=>{
return product.type==='beauty'
}).map((product)=>{
return product.name
})
console.log(search)
//que3
//map array of obj and display on div
let lastname=[
{name:"kamran",type:"boy"},
{name:"bilal",type:"boy"},
{name:"alina",type:"girl"},
{name:"momina",type:"girl"},
]
// lastname.map((name)=>{
// return (
// <div> <h1>{name.type} </h1> </div>
// )
// })
//que4
//
let relatives=[
{name:"phupo",percen:"1%",type:"notimmed"},
{name:"mamoo",percen:"30%",type:"notimmed"},
{name:"khala",percen:"80%",type:"notimmed"},
{name:"father",percen:"0%",type:"immed"},
]
let object={}
relatives.forEach((relative)=>{
if(object[relative.type]){
object[relative.type].push(relative)
}else{
object[relative.type]=[]
object[relative.type].push(relative)
}
})
console.log(object)
//que5
//if you want to remove an obj from array
let countries=[
{name:"Japan",id:1},
{name:"China",id:2},
{name:"Canda",id:3},
{name:"Korea",id:4},
]
let updated_countries=countries.filter((country)=>{
return country.id!==3
})
console.log(updated_countries)