-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDestructuring.js
More file actions
46 lines (38 loc) · 985 Bytes
/
Copy pathDestructuring.js
File metadata and controls
46 lines (38 loc) · 985 Bytes
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
// unpacking object array
const actor={
name:'Ananta',
age:30,
phone:123456,
money:100000
}
// we can get the items with the actual name in this way
// use property name as a variable that contains the property
const {age}=actor;
console.log(age);
const {phone,money}=actor;
console.log(phone,money);
// array destructuring
const numbers=[1,2,3,4];
const [first,second]=numbers;
console.log(first,second);
const [x,y]=[1,2];
console.log(x,y);
// if its an array u have to destructure them using the same object array
const double=(x,y)=>[x*2,y*2];
const [c,d]=double(2,3);
let [num1,,,num2]=[1,2,3,4];
console.log(num1,num2);
const key=Object.keys(actor);
console.log(key);
const values=Object.entries(actor);
console.log(values);
// delete object
delete actor.age;
console.log(actor);
// take all the other elements except name
const {name,...rest}=actor;
console.log(rest);
// stop the update
Object.freeze(actor);
actor.wife='Sharddha';
console.log(actor);