From edfe91ab4e48d06385bea87998cfa92b3a1c1140 Mon Sep 17 00:00:00 2001 From: Harshil Date: Tue, 8 Dec 2020 18:25:40 +0530 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=94=A8Update=20data=20structure=20for?= =?UTF-8?q?=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/reference/javascript-code-snippets.md | 171 ++++++++++++--------- 1 file changed, 101 insertions(+), 70 deletions(-) diff --git a/docs/reference/javascript-code-snippets.md b/docs/reference/javascript-code-snippets.md index 0d4d32ecd..bc46b7bf2 100644 --- a/docs/reference/javascript-code-snippets.md +++ b/docs/reference/javascript-code-snippets.md @@ -469,21 +469,19 @@ You can sort the data based on a field that has numerical values using the Funct If the data structure of the incoming data is similar to the following. ```js -[ - [ - { - "name": "Nathan", - "id": 3 - }, - { - "name": "n8n", - "id": 1 - }, - { - "name": "nodemation", - "id": 2 - } - ] + [ + { + "name": "Stefan", + "id": 3 + }, + { + "name": "Jim", + "id": 1 + }, + { + "name": "Hans", + "id": 2 + } ] ``` @@ -492,8 +490,8 @@ If the data structure of the incoming data is similar to the following. To sort the data in an ascending order use the following code snippet in the Function node. ```js -const sortedArr = items[0].json.sort((a, b) => { - return a.id - b.id; +const sortedArr = items.sort((a, b) => { + return a.json.id - b.json.id; }) return [{json:sortedArr}] ``` @@ -504,16 +502,25 @@ The output will then be similar to the following. [ [ { - "name": "n8n", - "id": 1 + "json": + { + "name": "Jim", + "id": 1 + } }, { - "name": "nodemation", - "id": 2 + "json": + { + "name": "Hans", + "id": 2 + } }, { - "name": "Nathan", - "id": 3 + "json": + { + "name": "Stefan", + "id": 3 + } } ] ] @@ -525,8 +532,8 @@ You can also use this example [workflow](https://n8n.io/workflows/801). To sort the data in descending order use the following code snippet in the Function node. ```js -const sortedArr = items[0].json.sort((a, b) => { - return b.id - a.id; +const sortedArr = items.sort((a, b) => { + return b.json.id - a.json.id; }) return [{json:sortedArr}] ``` @@ -534,19 +541,28 @@ return [{json:sortedArr}] The output will then be similar to the following. ```js - [ - [ +[ + [ { - "name": "Nathan", - "id": 3 + "json": + { + "name": "Stefan", + "id": 3 + } }, { - "name": "nodemation", - "id": 2 + "json": + { + "name": "Hans", + "id": 2 + } }, { - "name": "n8n", - "id": 1 + "json": + { + "name": "Jim", + "id": 1 + } } ] ] @@ -559,21 +575,19 @@ You can sort the data based on a field that has string values using the Function If the data structure of the incoming data is similar to the following. ```js -[ - [ - { - "name": "Munich", - "id": 3 - }, - { - "name": "Berlin", - "id": 1 - }, - { - "name": "Paris", - "id": 2 - } - ] + [ + { + "name": "Jim", + "id": 3 + }, + { + "name": "Stefan", + "id": 1 + }, + { + "name": "Hans", + "id": 2 + } ] ``` @@ -582,9 +596,9 @@ If the data structure of the incoming data is similar to the following. To sort the data in ascending order use the following code snippet in the Function node. ```js -const sortedArr = items[0].json.sort((a, b) => { - let a_name = a.name.toLowerCase(), - b_name = b.name.toLowerCase(); +const sortedArr = items.sort((a, b) => { + let a_name = a.json.name.toLowerCase(), + b_name = b.json.name.toLowerCase(); if (a_name < b_name) { return -1; @@ -603,16 +617,25 @@ The output will then be similar to the following. [ [ { - "name": "Berlin", - "id": 1 + "json": + { + "name": "Hans", + "id": 2 + } }, { - "name": "Munich", - "id": 3 + "json": + { + "name": "Jim", + "id": 3 + } }, { - "name": "Paris", - "id": 2 + "json": + { + "name": "Stefan", + "id": 1 + } } ] ] @@ -624,9 +647,9 @@ You can also use this example [workflow](https://n8n.io/workflows/803). To sort the data in descending order use the following code snippet in the Function node. ```js -const sortedArr = items[0].json.sort((a, b) => { - let a_name = a.name.toLowerCase(), - b_name = b.name.toLowerCase(); +const sortedArr = items.sort((a, b) => { + let a_name = a.json.name.toLowerCase(), + b_name = b.json.name.toLowerCase(); if (a_name > b_name) { return -1; @@ -636,26 +659,34 @@ const sortedArr = items[0].json.sort((a, b) => { } return 0; }); -return [{json:sortedArr}] - +return [{json:sortedArr}]; ``` The output will then be similar to the following. ```js - [ - [ +[ + [ { - "name": "Paris", - "id": 2 + "json": + { + "name": "Stefan", + "id": 1 + } }, { - "name": "Munich", - "id": 3 + "json": + { + "name": "Jim", + "id": 3 + } }, { - "name": "Berlin", - "id": 1 + "json": + { + "name": "Hans", + "id": 2 + } } ] ] From 82824e9dd7e2b6faa754ab3647ce9e7b41e2f5ea Mon Sep 17 00:00:00 2001 From: Harshil Date: Tue, 15 Dec 2020 15:02:19 +0530 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=94=A8Fix=20modify=20data=20structure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/reference/javascript-code-snippets.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/reference/javascript-code-snippets.md b/docs/reference/javascript-code-snippets.md index bc46b7bf2..55bd05fe8 100644 --- a/docs/reference/javascript-code-snippets.md +++ b/docs/reference/javascript-code-snippets.md @@ -316,9 +316,9 @@ false Depending on your use-case, you might want to convert the structure of the incoming data. You can use the Function node to change the data structure of the incoming data. Please note that you might have to make some changes to the code based on your data. To know more about the data structure in n8n, please refer to the [Data Structure](./data/data-structure.md) page. -### 1. Create multiple JSON items from an array +### 1. Create multiple items from a single item -If the data structure of the incoming data is similar to the following. +If the incoming data consists of a single item , similar to the following. ```js [ @@ -335,7 +335,7 @@ If the data structure of the incoming data is similar to the following. ] ] ``` -You can use the following code snippet to convert the array to multiple JSON items. +You can use the following code snippet to create multiple items from a single item. ```js return items[0].json.map(item => { @@ -345,7 +345,7 @@ return items[0].json.map(item => { }); ``` -The output will then be similar to the following. +The above code snippet will create multiple items and the output will then be similar to the following. ```js [ @@ -363,9 +363,9 @@ The output will then be similar to the following. You can also use this example [workflow](https://n8n.io/workflows/766). -### 2. Create an array of objects +### 2. Create a single item from multiple items -If the data structure of the incoming data is similar to the following. +If the incoming data consists of multiple items, similar to the following. ```js [ @@ -381,7 +381,7 @@ If the data structure of the incoming data is similar to the following. ] ``` -You can use the following code snippet to create an array of objects. +You can use the following code snippet to create a single item from these multiple items. ```js return [ @@ -393,7 +393,7 @@ You can use the following code snippet to create an array of objects. ] ``` -The output will then be similar to the following. +The above code snippet will create a single item and the output will be similar to the following. ```js [ From f7e258548a20ecb8c6ba5d4121612f2d93d5bc94 Mon Sep 17 00:00:00 2001 From: Harshil Date: Tue, 15 Dec 2020 15:40:49 +0530 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=94=A8Fix=20sort=20code=20snippets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/reference/javascript-code-snippets.md | 204 +++++++++------------ 1 file changed, 82 insertions(+), 122 deletions(-) diff --git a/docs/reference/javascript-code-snippets.md b/docs/reference/javascript-code-snippets.md index 55bd05fe8..7703ad470 100644 --- a/docs/reference/javascript-code-snippets.md +++ b/docs/reference/javascript-code-snippets.md @@ -318,7 +318,9 @@ Depending on your use-case, you might want to convert the structure of the incom ### 1. Create multiple items from a single item -If the incoming data consists of a single item , similar to the following. +If you receive a single item from a node, you can split the data into individual items. + +For example, if your incoming data is similar to the following. ```js [ @@ -345,7 +347,7 @@ return items[0].json.map(item => { }); ``` -The above code snippet will create multiple items and the output will then be similar to the following. +The above code snippet will create multiple items and the output will be similar to the following. ```js [ @@ -365,23 +367,25 @@ You can also use this example [workflow](https://n8n.io/workflows/766). ### 2. Create a single item from multiple items -If the incoming data consists of multiple items, similar to the following. +If you receive multiple items from a node, you can create a single item. + +For example, if your incoming data is similar to the following. ```js [ { - "item": "item-1" + "data": "item-1" }, { - "item": "item-2" + "data": "item-2" }, { - "item": "item-3" + "data": "item-3" } ] ``` -You can use the following code snippet to create a single item from these multiple items. +You can use the following code snippet. ```js return [ @@ -400,13 +404,13 @@ The above code snippet will create a single item and the output will be similar { data_object: [ { - "item": "item-1" + "data": "item-1" }, { - "item": "item-2" + "data": "item-2" }, { - "item": "item-3" + "data": "item-3" } ] } @@ -461,13 +465,13 @@ The output will then be similar to the following. ] ``` -## Sort data -Depending on your use-case, you might want to sort the data returned by the last node. You can sort the data based on the integer values (for example, id or age) or string (for example, name). +## Sort items +Depending on your use-case, you might want to sort the items returned by the last node. You can sort the items based on the integer values (for example, id or age) or string (for example, name). -### 1. Sort data based on an integer value -You can sort the data based on a field that has numerical values using the Function node. +### 1. Sort items based on an integer value +You can sort the items based on a field that has numerical values using the Function node. -If the data structure of the incoming data is similar to the following. +If the incoming data is similar to the following. ```js [ { @@ -485,94 +489,72 @@ If the data structure of the incoming data is similar to the following. ] ``` -#### 1. Sort the data in ascending order +#### 1. Sort the items in ascending order -To sort the data in an ascending order use the following code snippet in the Function node. +To sort the items in an ascending order, use the following code snippet in the Function node. ```js const sortedArr = items.sort((a, b) => { return a.json.id - b.json.id; }) -return [{json:sortedArr}] +return sortedArr ``` The output will then be similar to the following. ```js [ - [ - { - "json": - { - "name": "Jim", - "id": 1 - } - }, - { - "json": - { - "name": "Hans", - "id": 2 - } - }, - { - "json": - { - "name": "Stefan", - "id": 3 - } - } - ] + { + "name": "Jim", + "id": 1 + }, + { + "name": "Hans", + "id": 2 + }, + { + "name": "Stefan", + "id": 3 + } ] ``` You can also use this example [workflow](https://n8n.io/workflows/801). -#### 2. Sort the data in descending order +#### 2. Sort the items in descending order -To sort the data in descending order use the following code snippet in the Function node. +To sort the items in descending order use the following code snippet in the Function node. ```js const sortedArr = items.sort((a, b) => { return b.json.id - a.json.id; }) -return [{json:sortedArr}] +return sortedArr ``` The output will then be similar to the following. ```js [ - [ - { - "json": - { - "name": "Stefan", - "id": 3 - } - }, - { - "json": - { - "name": "Hans", - "id": 2 - } - }, - { - "json": - { - "name": "Jim", - "id": 1 - } - } - ] + { + "name": "Stefan", + "id": 3 + }, + { + "name": "Hans", + "id": 2 + }, + { + "name": "Jim", + "id": 1 + } ] ``` You can also use this example [workflow](https://n8n.io/workflows/802). -### 2. Sort data based on the string values -You can sort the data based on a field that has string values using the Function node. +### 2. Sort items based on the string values +You can sort the items based on a field that has string values using the Function node. -If the data structure of the incoming data is similar to the following. +If the incoming data is similar to the following. ```js [ @@ -591,9 +573,9 @@ If the data structure of the incoming data is similar to the following. ] ``` -#### 1. Sort the data in ascending order +#### 1. Sort the items in ascending order -To sort the data in ascending order use the following code snippet in the Function node. +To sort the items in ascending order use the following code snippet in the Function node. ```js const sortedArr = items.sort((a, b) => { @@ -608,43 +590,32 @@ const sortedArr = items.sort((a, b) => { } return 0; }); -return [{json:sortedArr}]; +return json:sortedArr; ``` The output will then be similar to the following. ```js [ - [ - { - "json": - { - "name": "Hans", - "id": 2 - } - }, - { - "json": - { - "name": "Jim", - "id": 3 - } - }, - { - "json": - { - "name": "Stefan", - "id": 1 - } - } - ] + { + "name": "Hans", + "id": 2 + }, + { + "name": "Jim", + "id": 3 + }, + { + "name": "Stefan", + "id": 1 + } ] ``` You can also use this example [workflow](https://n8n.io/workflows/803). -#### 2. Sort the data in descending order +#### 2. Sort the items in descending order -To sort the data in descending order use the following code snippet in the Function node. +To sort the items in descending order use the following code snippet in the Function node. ```js const sortedArr = items.sort((a, b) => { @@ -659,36 +630,25 @@ const sortedArr = items.sort((a, b) => { } return 0; }); -return [{json:sortedArr}]; +return sortedArr; ``` The output will then be similar to the following. ```js [ - [ - { - "json": - { - "name": "Stefan", - "id": 1 - } - }, - { - "json": - { - "name": "Jim", - "id": 3 - } - }, - { - "json": - { - "name": "Hans", - "id": 2 - } - } - ] + { + "name": "Stefan", + "id": 1 + }, + { + "name": "Jim", + "id": 3 + }, + { + "name": "Hans", + "id": 2 + } ] ``` You can also use this example [workflow](https://n8n.io/workflows/804). From be7f72ae581918f5b95b5f41a46344ea7f85ca1a Mon Sep 17 00:00:00 2001 From: Harshil Date: Tue, 15 Dec 2020 16:04:58 +0530 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=94=A8Fix=20mock=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/reference/javascript-code-snippets.md | 148 +++++++++++---------- 1 file changed, 80 insertions(+), 68 deletions(-) diff --git a/docs/reference/javascript-code-snippets.md b/docs/reference/javascript-code-snippets.md index 7703ad470..74f973fec 100644 --- a/docs/reference/javascript-code-snippets.md +++ b/docs/reference/javascript-code-snippets.md @@ -326,13 +326,16 @@ For example, if your incoming data is similar to the following. [ [ { - "data": "item-1", + "id": 1, + "name": "Jim" }, { - "data": "item-2", + "id": 2, + "name": "Stefan" }, { - "data": "item-3", + "id": 3, + "name": "Hans" } ] ] @@ -352,13 +355,16 @@ The above code snippet will create multiple items and the output will be similar ```js [ { - "data": "item-1" + "id": 1, + "name": "Jim" }, { - "data": "item-2" + "id": 2, + "name": "Stefan" }, { - "data": "item-3" + "id": 3, + "name": "Hans" } ] ``` @@ -374,13 +380,16 @@ For example, if your incoming data is similar to the following. ```js [ { - "data": "item-1" + "id": 1, + "name": "Jim" }, { - "data": "item-2" + "id": 2, + "name": "Stefan" }, { - "data": "item-3" + "id": 3, + "name": "Hans" } ] ``` @@ -404,13 +413,16 @@ The above code snippet will create a single item and the output will be similar { data_object: [ { - "data": "item-1" + "id": 1, + "name": "Jim" }, { - "data": "item-2" + "id": 2, + "name": "Stefan" }, { - "data": "item-3" + "id": 3, + "name": "Hans" } ] } @@ -475,29 +487,29 @@ If the incoming data is similar to the following. ```js [ { - "name": "Stefan", - "id": 3 + "id": 3, + "name": "Stefan" }, { - "name": "Jim", - "id": 1 + "id": 1, + "name": "Jim" }, { - "name": "Hans", - "id": 2 + "id": 2, + "name": "Hans" } ] ``` #### 1. Sort the items in ascending order -To sort the items in an ascending order, use the following code snippet in the Function node. +To sort the items in ascending order, use the following code snippet in the Function node. ```js const sortedArr = items.sort((a, b) => { return a.json.id - b.json.id; }) -return sortedArr +return sortedArr; ``` The output will then be similar to the following. @@ -505,16 +517,16 @@ The output will then be similar to the following. ```js [ { - "name": "Jim", - "id": 1 + "id": 1, + "name": "Jim" }, { - "name": "Hans", - "id": 2 + "id": 2, + "name": "Hans" }, { - "name": "Stefan", - "id": 3 + "id": 3, + "name": "Stefan" } ] ``` @@ -528,7 +540,7 @@ To sort the items in descending order use the following code snippet in the Func const sortedArr = items.sort((a, b) => { return b.json.id - a.json.id; }) -return sortedArr +return sortedArr; ``` The output will then be similar to the following. @@ -536,16 +548,16 @@ The output will then be similar to the following. ```js [ { - "name": "Stefan", - "id": 3 + "id": 3, + "name": "Stefan" }, { - "name": "Hans", - "id": 2 + "id": 2, + "name": "Hans" }, { - "name": "Jim", - "id": 1 + "id": 1, + "name": "Jim" } ] ``` @@ -559,16 +571,16 @@ If the incoming data is similar to the following. ```js [ { - "name": "Jim", - "id": 3 + "id": 3, + "name": "Jim" }, { - "name": "Stefan", - "id": 1 + "id": 1, + "name": "Stefan" }, { - "name": "Hans", - "id": 2 + "id": 2, + "name": "Hans" } ] ``` @@ -598,16 +610,16 @@ The output will then be similar to the following. ```js [ { - "name": "Hans", - "id": 2 + "id": 2, + "name": "Hans" }, { - "name": "Jim", - "id": 3 + "id": 3, + "name": "Jim" }, { - "name": "Stefan", - "id": 1 + "id": 1, + "name": "Stefan" } ] ``` @@ -638,16 +650,16 @@ The output will then be similar to the following. ```js [ { - "name": "Stefan", - "id": 1 + "id": 1, + "name": "Stefan" }, { - "name": "Jim", - "id": 3 + "id": 3, + "name": "Jim" }, { - "name": "Hans", - "id": 2 + "id": 2, + "name": "Hans" } ] ``` @@ -707,28 +719,28 @@ If the data structure of the incoming data is similar to the following. { "data": [ { - "name": "n8n", - "id": 1 + "id": 1, + "name": "n8n" }, { - "name": "Nathan", - "id": 2 + "id": 2, + "name": "Nathan" }, { - "name": "nodemation", - "id": 3 + "id": 3, + "name": "nodemation" }, { - "name": "Nathan", - "id": 4 + "id": 4, + "name": "Nathan" }, { - "name": "n8n", - "id": 5 + "id": 5, + "name": "n8n" }, { - "name": "Berlin", - "id": 6 + "id": 6, + "name": "Berlin" } ] } @@ -757,20 +769,20 @@ The output will then be similar to the following. [ [ { - "name": "n8n", - "id": 5 + "id": 5, + "name": "n8n" }, { + "id": 4, "name": "Nathan", - "id": 4 }, { + "id": 3, "name": "nodemation", - "id": 3 }, { + "id": 6, "name": "Berlin", - "id": 6 } ] ] From 1129da5865cc25d38295b1a6a0d5f5722e2d3695 Mon Sep 17 00:00:00 2001 From: Tanay Pant Date: Tue, 15 Dec 2020 12:26:32 +0100 Subject: [PATCH 5/5] :hammer: Minor fix --- docs/reference/javascript-code-snippets.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/javascript-code-snippets.md b/docs/reference/javascript-code-snippets.md index 74f973fec..90ac3c3c3 100644 --- a/docs/reference/javascript-code-snippets.md +++ b/docs/reference/javascript-code-snippets.md @@ -312,7 +312,7 @@ The expression would resolve to something similar to the following. false ``` -## Modify data Structure +## Modify Data Structure Depending on your use-case, you might want to convert the structure of the incoming data. You can use the Function node to change the data structure of the incoming data. Please note that you might have to make some changes to the code based on your data. To know more about the data structure in n8n, please refer to the [Data Structure](./data/data-structure.md) page.