-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoopingTechnics.js
184 lines (122 loc) · 2.37 KB
/
LoopingTechnics.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
172
173
174
175
176
177
178
179
180
181
182
183
184
//map
//reduce
//for in
//for of
//
const arr=[1,2,3,4,5];
const a=null;
const b=2;
const c= a||b;
const d= a&&b;
//and condition is equal to if condition
console.log(c);
console.log(d);
// let sum=0;
// for (let i = 0; i < arr.length; i++) {
// sum=sum+arr[i];
// };
// console.log(sum)
//first perameter is function //second perameteris initial value
// const sum=arr.reduce((previous,current)=>{
// console.log(previous);
// console.log(current);
// return previous+current;
// // return 2;
// },0);
const sum=arr.reduce((p,c)=>p+c,0);//shorter way to write
console.log(sum)
// const obj={
// a:1,
// b:2,
// c:3,
// }
// // const d= 'd';
// console.log(obj.[d]);
//es6
const users=[{
id:1,
name:'ansh',
gender:'Male',
},
{
id:2,
name:'Doshi',
gender:'Male',
},
{
id:3,
name:'kiara',
gender:'Female',
},
{
id:4,
name:'alia',
gender:'Female',
}];
const groupBy=users.reduce((p,c)=>{
(p[c.gender]=p[c.gender]||[]).push(c);
return p;
},{})
console.log(groupBy)
// const updateduserlist=users.reduce((p,c)=>{
// if(c.id===1){
// return [...p,{...c,name:'ANSHU'}]
// };
// return [...p,c];
// },[])
// console.log(updateduserlist);
// {
// male:[/*all the males*/],
// male:[/*all the females*/],
// }
//reduce loop(most powerfull loop)
//100000
//avaible at index:2
//max time process atre traversing,iteration
//2 times
const index=users.findIndex((value)=>{
console.log(value.id)
return value.id===1;
});
console.log(index);
//up
const updatedUsers=[
...users.slice(0,index),//take all record before index
{...users[index],name:'Ansh'},//updated object
...users.slice(index+1),//take all record after index
];
const updatedUsers1=users.map((v,i)=>{
console.log(v.id)
if(v.id===1){
return {...v,name:'Anshu'};
}
return v;
}
)
console.log(updatedUsers1);
console.log(updatedUsers);
//forin
const obj={
a:1,
b:2,
c:3,
};
for (const key in obj) {
if (Object.hasOwnProperty.call(obj, key)) {
const element = obj[key];
console.log(key);
console.log(element);
console.log(obj);
}
}
//forof
const arry={
a:1,
b:2,
c:3,
};
for (const [key,value] of Object.entries(arry)) {
console.log(key);
console.log(value);
console.log(arry);
}