You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| 1 |[Add a key-value pair in object conditionally](#add-a-key-value-pair-in-object-conditionally)|
14
+
| 2 |[Deleting a particular key from object](#deleting-a-particular-key-from-object)|
14
15
15
16
16
17
---
@@ -30,19 +31,19 @@ One liner clean code snippets in javascript/react with ES6 syntax
30
31
roll:'AD203',
31
32
stream:'MERN stack web development',
32
33
...(is_employee && {company:'Inventives.ai})
33
-
}
34
+
};
34
35
35
36
if(is_employee){
36
37
student.company='Inventives.ai';
37
-
}
38
+
};
38
39
```
39
40
40
41
41
42
We will use Spread operator to spread new object with one property inside the previously defined object and the conditional operator `&&` to achieve the task conditionally
42
43
43
44
1. **While defining the object:**
44
45
45
-
We are injecting the new property at time of defining the object only
46
+
-- We are injecting the new property at time of defining the object only
46
47
47
48
```javascript
48
49
const is_employee=true;
@@ -52,28 +53,67 @@ One liner clean code snippets in javascript/react with ES6 syntax
52
53
roll:'AD203',
53
54
stream:'MERN stack web development',
54
55
...(is_employee && {company:'Inventives.ai})
55
-
}
56
+
};
56
57
```
57
58
58
59
2.**After object has been defined**
59
60
60
-
We are injecting the newproperty after the object has been defined with its default key value pairs.
61
+
-- We are injecting the newproperty after the object has been defined with its default key value pairs.
61
62
62
63
```javascript
63
64
const is_employee=true;
64
65
let student={
65
66
name:'Krishna Kant',
66
67
roll:'AD203'
67
-
}
68
+
};
69
+
68
70
student={
69
71
...student,
70
72
...(is_employee && {company: 5})
71
-
}
73
+
};
72
74
```
73
75
74
76
**[⬆ Back to Top](#table-of-contents)**
75
77
76
78
79
+
80
+
2. ### Deleting a particular key from object
81
+
82
+
83
+
84
+
**Plain conditional operation**
85
+
86
+
```javascript
87
+
const is_employee=true;
88
+
89
+
const student={
90
+
name:'Krishna Kant',
91
+
roll:'AD203',
92
+
stream:'MERN stack web development',
93
+
company:'Inventives.ai'
94
+
};
95
+
96
+
delete student.salary;
97
+
```
98
+
99
+
100
+
-- We will use Spread operator to delete one property inside the previously defined object.
101
+
102
+
```javascript
103
+
let student={
104
+
name:'Krishna Kant',
105
+
roll:'AD203',
106
+
stream:'MERN stack web development',
107
+
company:'Inventives.ai'
108
+
};
109
+
110
+
let {company, ...newStudent} = student;
111
+
student=newStudent;
112
+
113
+
```
114
+
115
+
116
+
77
117
## Disclaimer
78
118
79
119
The code snippets mentioned inthis repository are the summary of frequently used codes in Javascript and/or React withES6syntax. We cannot guarantee that only these snippets will actually make the code cleaner, smoother and optimised, nor should you focus on copying and pasting them all. The primary purpose is for you to get a sense of how some bulk codes might be replaced with one-liner ES6 conditional and operational statements!
0 commit comments