Skip to content

Commit fc2b190

Browse files
authored
Merge pull request #1 from kkm980/test_branch
Test branch
2 parents 3653538 + e629e6b commit fc2b190

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,84 @@
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+
---
17+
18+
19+
1. ### Add a key-value pair in object conditionally
20+
21+
22+
23+
**Plain conditional operation**
24+
25+
```javascript
26+
const is_employee=true;
27+
28+
const student={
29+
name:'Krishna Kant',
30+
roll:'AD203',
31+
stream:'MERN stack web development',
32+
...(is_employee && {company:'Inventives.ai})
33+
}
34+
35+
if(is_employee){
36+
student.company='Inventives.ai';
37+
}
38+
```
39+
40+
41+
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+
1. **While defining the object:**
44+
45+
We are injecting the new property at time of defining the object only
46+
47+
```javascript
48+
const is_employee=true;
49+
50+
const student={
51+
name:'Krishna Kant',
52+
roll:'AD203',
53+
stream:'MERN stack web development',
54+
...(is_employee && {company:'Inventives.ai})
55+
}
56+
```
57+
58+
2. **After object has been defined**
59+
60+
We are injecting the new property after the object has been defined with its default key value pairs.
61+
62+
```javascript
63+
const is_employee=true;
64+
let student={
65+
name:'Krishna Kant',
66+
roll:'AD203'
67+
}
68+
student={
69+
...student,
70+
...(is_employee && {company: 5})
71+
}
72+
```
73+
74+
**[⬆ Back to Top](#table-of-contents)**
75+
76+
77+
## Disclaimer
78+
79+
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!
80+
81+
82+
Happy Coding 😊
83+
84+
---
85+

0 commit comments

Comments
 (0)