Skip to content

Commit 87912f2

Browse files
committed
foreach,map,filter,spread,rest,destructuring
1 parent 9894f8d commit 87912f2

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

revision/rev4.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//for each
2+
3+
let names=['alice','bob','peter','jane'];
4+
5+
names.forEach((name)=>{
6+
console.log(name+" ji");
7+
8+
})
9+
10+
//map
11+
let newnames=names.map((name)=>{
12+
return name;
13+
})
14+
15+
console.log(newnames);
16+
17+
//filter
18+
let condnames=names.filter((name)=>{
19+
return name==='alice'
20+
})
21+
console.log(condnames)
22+
23+
//destructuring
24+
let obj={
25+
name:"marry",
26+
age:[20,30,40],
27+
address:{
28+
karachi:"gulshan",
29+
lahore:"laal qila"
30+
},
31+
country:"Pakistan"
32+
}
33+
34+
let access1=obj.age.at(0)
35+
let access2=obj.address.karachi
36+
console.log(access1);
37+
console.log(access2);
38+
39+
let {karachi,lahore} =obj.address
40+
console.log(lahore);
41+
42+
let [f,s] =obj.age
43+
console.log(s)
44+
45+
//spread
46+
//premetives pass by value
47+
let a=4;
48+
let b=a;
49+
console.log(b+1);
50+
console.log(a);
51+
52+
//nonpremitives pass by ref
53+
let array1=[1,2,3]
54+
// let array2=array1
55+
56+
// array2.pop()
57+
// console.log(array2);
58+
// console.log(array1);
59+
60+
//to solve this prob we use spread op
61+
let array2=[...array1];
62+
array2.pop()
63+
console.log(array2);
64+
console.log(array1);
65+
66+
//same for object
67+
let obj1={
68+
name:"hello",
69+
color:"black"
70+
}
71+
let obj2={...obj1}
72+
obj2.name="mak";
73+
console.log(obj1)
74+
console.log(obj2)
75+
76+
//spread op is only level one operator (que asked in interview)
77+
78+
let list={
79+
add:{
80+
name:"mak"
81+
},
82+
tag:"sad"
83+
}
84+
let list2={...list}
85+
list2.add.name="fak";
86+
console.log(list)
87+
console.log(list2)
88+
89+
//rest op
90+
//bajay ye k ap multiple var banay in fun par we can store val in an array code optimization and memorysaves wrt space
91+
92+
let bilal =function(...val){
93+
console.log(val)
94+
}
95+
bilal(1,2,3,4);
96+
97+
98+

0 commit comments

Comments
 (0)