From cdb3bbcf1d1d1302ad3be04293daf5a554912e23 Mon Sep 17 00:00:00 2001 From: krishna9802 Date: Sat, 28 Jan 2023 09:50:55 +0530 Subject: [PATCH 1/6] first code snippet addded --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/README.md b/README.md index a1e3b55..7016eb1 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,60 @@ One liner clean code snippets in javascript/react with ES6 syntax +> Click :star:if you like the project and follow [@KrishnaKant](https://www.linkedin.com/in/krishna980/) for more updates. + +--- + +### Table of Contents + +| No. | Code | +| --- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| 1 | [Add a key-value pair in object conditionally](#add-a-key-value-pair-in-object-conditionally) | + | + +1. ### Add a key-value pair in object conditionally + + Spread operator is used to spread one object and conditional operator && to achieve the task conditionally + + 1. **While defining the object:** + + We are injecting the new property at time of defining the object only + + ```javascript + const is_employee=true; + const student={ + name:'Krishna Kant', + roll:'AD203', + stream:'MERN stack web development', + ...(is_employee && {company:'Inventives.ai}) + } + ``` + + 2. **After object has been defined** + + We are injecting the new property after the object has been defined with its default key value pairs. + + ```javascript + const is_employee=true; + let student={ + name:'Krishna Kant', + roll:'AD203' + } + is_employee && student={ + ...student, + current_company:'Inventives.ai' + } + ``` + + **[⬆ Back to Top](#table-of-contents)** + + +## Disclaimer + +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! + + +Happy Coding 😊 + +--- + From 5d79fa2dea8ebc9d7f08f7ca58794c41ff97b3b1 Mon Sep 17 00:00:00 2001 From: krishna9802 Date: Sat, 28 Jan 2023 09:52:07 +0530 Subject: [PATCH 2/6] first code snippet addded --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7016eb1..1ce522c 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ One liner clean code snippets in javascript/react with ES6 syntax } is_employee && student={ ...student, - current_company:'Inventives.ai' + company:'Inventives.ai' } ``` From 418c77a4978ec26665a526e09ef672ada0f5d6fa Mon Sep 17 00:00:00 2001 From: krishna9802 Date: Sat, 28 Jan 2023 10:07:25 +0530 Subject: [PATCH 3/6] first code snippet addded --- README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 1ce522c..d6ef08d 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,14 @@ One liner clean code snippets in javascript/react with ES6 syntax We are injecting the new property at time of defining the object only ```javascript - const is_employee=true; - const student={ + const is_employee=true; + + const student={ name:'Krishna Kant', roll:'AD203', stream:'MERN stack web development', ...(is_employee && {company:'Inventives.ai}) - } + } ``` 2. **After object has been defined** @@ -38,13 +39,13 @@ One liner clean code snippets in javascript/react with ES6 syntax ```javascript const is_employee=true; let student={ - name:'Krishna Kant', - roll:'AD203' + name:'Krishna Kant', + roll:'AD203' + } + student={ + ...student, + ...(is_employee && {company: 5}) } - is_employee && student={ - ...student, - company:'Inventives.ai' - } ``` **[⬆ Back to Top](#table-of-contents)** From 0358cbb34081cdf526a37d5b1675b20f34da581d Mon Sep 17 00:00:00 2001 From: krishna9802 Date: Sat, 28 Jan 2023 10:10:21 +0530 Subject: [PATCH 4/6] first code snippet addded --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d6ef08d..5a34e61 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ One liner clean code snippets in javascript/react with ES6 syntax | No. | Code | | --- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | | 1 | [Add a key-value pair in object conditionally](#add-a-key-value-pair-in-object-conditionally) | - | + + 1. ### Add a key-value pair in object conditionally From 6b872d05dec36eef57babf1b51c46fea8cf2925d Mon Sep 17 00:00:00 2001 From: krishna9802 Date: Sat, 28 Jan 2023 10:15:56 +0530 Subject: [PATCH 5/6] first code snippet addded --- README.md | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5a34e61..4c4390a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,27 @@ One liner clean code snippets in javascript/react with ES6 syntax 1. ### Add a key-value pair in object conditionally - Spread operator is used to spread one object and conditional operator && to achieve the task conditionally + + + **Plain conditional operation** + + ```javascript + const is_employee=true; + + const student={ + name:'Krishna Kant', + roll:'AD203', + stream:'MERN stack web development', + ...(is_employee && {company:'Inventives.ai}) + } + + if(is_employee){ + student.company='Inventives.ai'; + } + ``` + + + 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 1. **While defining the object:** @@ -33,7 +53,7 @@ One liner clean code snippets in javascript/react with ES6 syntax } ``` - 2. **After object has been defined** + 2. **After object has been defined** We are injecting the new property after the object has been defined with its default key value pairs. From e629e6b800e232f0c036bc06ecd423b2013e781e Mon Sep 17 00:00:00 2001 From: krishna9802 Date: Sat, 28 Jan 2023 10:18:13 +0530 Subject: [PATCH 6/6] first code snippet addded --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4c4390a..4d77b62 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,8 @@ One liner clean code snippets in javascript/react with ES6 syntax | 1 | [Add a key-value pair in object conditionally](#add-a-key-value-pair-in-object-conditionally) | +--- + 1. ### Add a key-value pair in object conditionally