Skip to content

Commit be75384

Browse files
authored
Merge pull request #2 from kkm980/test_branch
Test branch
2 parents fc2b190 + 586bd98 commit be75384

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

README.md

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ 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
---
1718

1819

1920
1. ### Add a key-value pair in object conditionally
20-
21-
2221

2322
**Plain conditional operation**
2423

@@ -30,19 +29,19 @@ One liner clean code snippets in javascript/react with ES6 syntax
3029
roll:'AD203',
3130
stream:'MERN stack web development',
3231
...(is_employee && {company:'Inventives.ai})
33-
}
32+
};
3433
3534
if(is_employee){
3635
student.company='Inventives.ai';
37-
}
36+
};
3837
```
3938
4039
4140
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
4241
4342
1. **While defining the object:**
4443
45-
We are injecting the new property at time of defining the object only
44+
- We are injecting the new property at time of defining the object only
4645
4746
```javascript
4847
const is_employee=true;
@@ -52,28 +51,69 @@ One liner clean code snippets in javascript/react with ES6 syntax
5251
roll:'AD203',
5352
stream:'MERN stack web development',
5453
...(is_employee && {company:'Inventives.ai})
55-
}
54+
};
5655
```
5756

5857
2. **After object has been defined**
5958

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

6261
```javascript
6362
const is_employee=true;
6463
let student={
6564
name:'Krishna Kant',
6665
roll:'AD203'
67-
}
66+
};
67+
6868
student={
6969
...student,
7070
...(is_employee && {company: 5})
71-
}
71+
};
7272
```
7373

7474
**[⬆ Back to Top](#table-of-contents)**
7575

7676

77+
78+
2. ### Deleting a particular key from object
79+
80+
81+
82+
**Plain conditional operation**
83+
84+
```javascript
85+
const is_employee=true;
86+
87+
const student={
88+
name:'Krishna Kant',
89+
roll:'AD203',
90+
stream:'MERN stack web development',
91+
company:'Inventives.ai'
92+
};
93+
94+
delete student.salary;
95+
```
96+
97+
98+
- We will use Spread operator to delete one property inside the previously defined object.
99+
100+
```javascript
101+
let student={
102+
name:'Krishna Kant',
103+
roll:'AD203',
104+
stream:'MERN stack web development',
105+
company:'Inventives.ai'
106+
};
107+
108+
let {company, ...newStudent} = student;
109+
student=newStudent;
110+
111+
```
112+
113+
**[⬆ Back to Top](#table-of-contents)**
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)