Skip to content

Commit cdb3bbc

Browse files
author
krishna9802
committed
first code snippet addded
1 parent 3653538 commit cdb3bbc

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,60 @@
22
One liner clean code snippets in javascript/react with ES6 syntax
33

44

5+
> Click :star:if you like the project and follow [@KrishnaKant](https://www.linkedin.com/in/krishna980/) for more updates.
6+
7+
---
8+
9+
### Table of Contents
10+
11+
| No. | Code |
12+
| --- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
13+
| 1 | [Add a key-value pair in object conditionally](#add-a-key-value-pair-in-object-conditionally) |
14+
|
15+
16+
1. ### Add a key-value pair in object conditionally
17+
18+
Spread operator is used to spread one object and conditional operator && to achieve the task conditionally
19+
20+
1. **While defining the object:**
21+
22+
We are injecting the new property at time of defining the object only
23+
24+
```javascript
25+
const is_employee=true;
26+
const student={
27+
name:'Krishna Kant',
28+
roll:'AD203',
29+
stream:'MERN stack web development',
30+
...(is_employee && {company:'Inventives.ai})
31+
}
32+
```
33+
34+
2. **After object has been defined**
35+
36+
We are injecting the new property after the object has been defined with its default key value pairs.
37+
38+
```javascript
39+
const is_employee=true;
40+
let student={
41+
name:'Krishna Kant',
42+
roll:'AD203'
43+
}
44+
is_employee && student={
45+
...student,
46+
current_company:'Inventives.ai'
47+
}
48+
```
49+
50+
**[⬆ Back to Top](#table-of-contents)**
51+
52+
53+
## Disclaimer
54+
55+
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!
56+
57+
58+
Happy Coding 😊
59+
60+
---
61+

0 commit comments

Comments
 (0)