Last active
March 7, 2023 07:12
-
-
Save RR-Helpdesk/f75f3ac795115e74f23b6b1806048dc8 to your computer and use it in GitHub Desktop.
JS Snippets for N8N
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####DEEP COPY AN OBJECT | |
```js | |
var copiedObj = JSON.parse(JSON.stringify(obj)); | |
``` | |
Explanation: In JavaScript objects, copying an object variable results in copying only the reference to the same object. Hence, any changes to its embedded key-pair values result in changes made to the original object variable as well. | |
```js | |
var obj = { a: 1, b: 2 }; | |
var copiedObj = obj; | |
copiedObj.a = 2; | |
console.log(copiedObj); // {a: 2, b: 2} | |
console.log(obj); // {a: 2, b: 2} | |
``` | |
To prevent changes from being made to the original object, consider using the following to deep copy an object variable instead (note that changes made to the new object variable no longer change elements in the original object): | |
```js | |
var obj = { a: 1, b: 2 }; | |
var copiedObj = JSON.parse(JSON.stringify(obj)); | |
copiedObj.a = 2; | |
console.log(copiedObj); // { a: 2, b: 2 } | |
console.log(obj); // { a: 1, b: 2 } | |
``` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####MERGE MULTIPLE ARRAYS INTO ONE | |
```js | |
var mergedArr = arr1.concat( arr2, arr3 ); | |
``` | |
Explanation: When you have multiple arrays you wish to merge instead, concatwould be a convenient means of doing so: | |
```js | |
var arr1 = [1,2,3]; | |
var arr2 = [4,5]; | |
var arr3 = [6]; | |
``` | |
var mergedArr = arr1.concat( arr2, arr3 ); // [1, 2, 3, 4, 5, 6] | |
Alternative: Using the (…) spread operator to concatenate arrays instead | |
Similar to how (...) was used in point 2, this can be applied to arrays rather than objects as well: | |
```js | |
var arr1 = [1,2,3]; | |
var arr2 = [4,5]; | |
var arr3 = [6]; | |
``` | |
```js | |
var mergedArr = [ ...arr1, ...arr2, ...arr3 ]; // [1, 2, 3, 4, 5, 6] | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####REMOVE THE 1ST ELEMENT OF ARRAY | |
```js | |
arr.shift(); | |
``` | |
Explanation: Rather than iterate through the entire array and transfer all elements excluding the 1st element into a newly initialised array ( [] ), simply use shift: | |
```js | |
var arr = ["first", "second", "third", "forth"]; | |
arr.shift(); | |
``` | |
// result of arr is: ["second", "third", "forth"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### REMOVE THE LAST ELEMENT OF THE ARRAY | |
```js | |
arr.pop(); | |
``` | |
Explanation: Similar to point 4, rather than iterate through the entire array and transfer all elements excluding the last element into a newly initialised array ( [] ), simply use pop: | |
```js | |
var arr = ["first", "second", "third", "forth"]; | |
arr.pop(); | |
``` | |
// result of arr is: ["first", "second", "third"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####REMOVE OBJECT KEY AND VALUE PAIR | |
```js | |
delete obj[keyValue]; | |
``` | |
Explanation: At times when you wish to remove a key-pair value from an object completely, rather than iterate through the object and assigning it to another empty object ( {} ), simply use the delete keyword: | |
```js | |
var obj = { id: 1, name: "ABC Primary School", category: "school" }; | |
``` | |
// assuming keyValue = "category": | |
```js | |
delete obj["category"] | |
``` | |
// obj now becomes { id: 1, name: "ABC Primary School" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### CODE SNIPPETS | |
<BR> | |
Remove The Last Element Of The Array | |
<br> | |
[[VIEW CODE]](snippetslab://snippet/774F9CBE-E351-4B36-BB5B-85A22DE53641/9B70577A-C686-4FAB-85C2-D6C84E91BC0B/) | |
Remove Object Key And Value Pair | |
<br> | |
[[VIEW CODE]](snippetslab://snippet/774F9CBE-E351-4B36-BB5B-85A22DE53641/6F6F16F0-46B9-494B-B3A7-463A933896BF/) | |
Merge Multiple Arrays Into One | |
<br> | |
[[VIEW CODE]](snippetslab://snippet/774F9CBE-E351-4B36-BB5B-85A22DE53641/6230D7E9-C4CC-46F8-943B-AE0F803933EB/) | |
Remove The 1St Element Of Array | |
<br> | |
[[VIEW CODE]](snippetslab://snippet/774F9CBE-E351-4B36-BB5B-85A22DE53641/5B055063-8CA8-42CB-A5AD-1315FEFDD01E/) | |
Merge Multiple Arrays Into One | |
<br> | |
[[VIEW CODE]](snippetslab://snippet/774F9CBE-E351-4B36-BB5B-85A22DE53641/6230D7E9-C4CC-46F8-943B-AE0F803933EB/) | |
Deep Copy An Object | |
<br> | |
[[VIEW CODE]](snippetslab://snippet/774F9CBE-E351-4B36-BB5B-85A22DE53641/CD55C08F-D148-466E-BA73-7A4A2DD32F89/) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment