From 979cd2a93abb1c1068e127b2abf4ab02b0ee1a5b Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Thu, 14 Dec 2017 18:41:38 +0330 Subject: [PATCH 1/4] adding function to clean json objects, deeply. --- snippets/clean-json-objects.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/clean-json-objects.md diff --git a/snippets/clean-json-objects.md b/snippets/clean-json-objects.md new file mode 100644 index 000000000..46ef745dc --- /dev/null +++ b/snippets/clean-json-objects.md @@ -0,0 +1,20 @@ +### Clean Json objects + +Clean your json object from unwanted keys, deeply. +provide set of `keys` to keep and an indicator for `children` if there is any. + +```js +const cleanObj = (obj, keys = [], childIndicator) => { + Object.keys(obj).forEach(key => { + if (key === childIndicator) { + cleanObj(obj[key], keys, childIndicator) + } else if (!keys.includes(key)) { + delete obj[key] + } + }) +} +/* + dirtyObj = { a: 1, b: 2, children: {a: 1, b :2}} + let cleaned = cleanObj(dirtyObj, [a]) // { a: 1, children : { a: 1}} + */ +``` From e9327cab11f6ecc4f2873cb0e28367e3f19725ac Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 15 Dec 2017 13:08:12 +0330 Subject: [PATCH 2/4] resolving some issues about comments --- snippets/clean-json-objects.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/snippets/clean-json-objects.md b/snippets/clean-json-objects.md index 46ef745dc..2835b37fd 100644 --- a/snippets/clean-json-objects.md +++ b/snippets/clean-json-objects.md @@ -14,7 +14,8 @@ const cleanObj = (obj, keys = [], childIndicator) => { }) } /* - dirtyObj = { a: 1, b: 2, children: {a: 1, b :2}} - let cleaned = cleanObj(dirtyObj, [a]) // { a: 1, children : { a: 1}} - */ + dirtyObj = {a: 1, b: 2, children: {a: 1, b: 2}} + cleanObj(dirtyObj, ["a"],"children") + console.log(dirtyObj)// { a: 1, children : { a: 1}} +*/ ``` From f7123086f70ce0cb280378e6cebf5b129728a3a3 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 15 Dec 2017 20:18:18 +0330 Subject: [PATCH 3/4] change description for clean json object function to explain more about it's inner workings. --- snippets/clean-json-objects.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/snippets/clean-json-objects.md b/snippets/clean-json-objects.md index 2835b37fd..5268e3911 100644 --- a/snippets/clean-json-objects.md +++ b/snippets/clean-json-objects.md @@ -1,7 +1,7 @@ ### Clean Json objects -Clean your json object from unwanted keys, deeply. -provide set of `keys` to keep and an indicator for `children` if there is any. +Use `Object.keys()` method to iter through given json object and deleting keys that are not `include`d in given array. +also if you give it a special key(`childIndicator`) it will search deeply inside it to apply function to inner objects too. ```js const cleanObj = (obj, keys = [], childIndicator) => { @@ -14,8 +14,8 @@ const cleanObj = (obj, keys = [], childIndicator) => { }) } /* - dirtyObj = {a: 1, b: 2, children: {a: 1, b: 2}} - cleanObj(dirtyObj, ["a"],"children") - console.log(dirtyObj)// { a: 1, children : { a: 1}} + testObj = {a: 1, b: 2, children: {a: 1, b: 2}} + cleanObj(testObj, ["a"],"children") + console.log(testObj)// { a: 1, children : { a: 1}} */ ``` From e642274382dd233efe4a4c816aede6ec9dc0c259 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 15 Dec 2017 20:23:23 +0330 Subject: [PATCH 4/4] refactoring grammatical problems and assigning better name to keys argument. --- snippets/clean-json-objects.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/clean-json-objects.md b/snippets/clean-json-objects.md index 5268e3911..5af7b6221 100644 --- a/snippets/clean-json-objects.md +++ b/snippets/clean-json-objects.md @@ -1,14 +1,14 @@ ### Clean Json objects -Use `Object.keys()` method to iter through given json object and deleting keys that are not `include`d in given array. +Use `Object.keys()` method to loop over given json object and deleting keys that are not `include`d in given array. also if you give it a special key(`childIndicator`) it will search deeply inside it to apply function to inner objects too. ```js -const cleanObj = (obj, keys = [], childIndicator) => { +const cleanObj = (obj, keysToKeep = [], childIndicator) => { Object.keys(obj).forEach(key => { if (key === childIndicator) { - cleanObj(obj[key], keys, childIndicator) - } else if (!keys.includes(key)) { + cleanObj(obj[key], keysToKeep, childIndicator) + } else if (!keysToKeep.includes(key)) { delete obj[key] } })