Skip to content

Commit c6b72fe

Browse files
author
krishna9802
committed
second code snippet addded
1 parent e629e6b commit c6b72fe

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

README.md

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ One liner clean code snippets in javascript/react with ES6 syntax
1111
| No. | Code |
1212
| --- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1313
| 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) |
1415

1516

1617
---
@@ -30,19 +31,19 @@ One liner clean code snippets in javascript/react with ES6 syntax
3031
roll:'AD203',
3132
stream:'MERN stack web development',
3233
...(is_employee && {company:'Inventives.ai})
33-
}
34+
};
3435
3536
if(is_employee){
3637
student.company='Inventives.ai';
37-
}
38+
};
3839
```
3940
4041
4142
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
4243
4344
1. **While defining the object:**
4445
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
4647
4748
```javascript
4849
const is_employee=true;
@@ -52,28 +53,67 @@ One liner clean code snippets in javascript/react with ES6 syntax
5253
roll:'AD203',
5354
stream:'MERN stack web development',
5455
...(is_employee && {company:'Inventives.ai})
55-
}
56+
};
5657
```
5758

5859
2. **After object has been defined**
5960

60-
We are injecting the new property after the object has been defined with its default key value pairs.
61+
-- We are injecting the new property after the object has been defined with its default key value pairs.
6162

6263
```javascript
6364
const is_employee=true;
6465
let student={
6566
name:'Krishna Kant',
6667
roll:'AD203'
67-
}
68+
};
69+
6870
student={
6971
...student,
7072
...(is_employee && {company: 5})
71-
}
73+
};
7274
```
7375

7476
**[⬆ Back to Top](#table-of-contents)**
7577

7678

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+
77117
## Disclaimer
78118

79119
The code snippets mentioned in this repository are the summary of frequently used codes in Javascript and/or React with ES6 syntax. 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

Comments
 (0)