From 3bcce60510f36860ddde9f9e0de851f0347eb18c Mon Sep 17 00:00:00 2001 From: taimoor Date: Tue, 22 Sep 2020 14:02:06 +0500 Subject: [PATCH 1/5] fix issue #1175 and cater more scenarios --- snippets/unflattenObject.md | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/snippets/unflattenObject.md b/snippets/unflattenObject.md index 5b7e10447..a2e757ac9 100644 --- a/snippets/unflattenObject.md +++ b/snippets/unflattenObject.md @@ -5,29 +5,27 @@ tags: object,advanced Unflatten an object with the paths for keys. -- Use `Object.keys(obj)` combined with `Array.prototype.reduce()` to convert flattened path node to a leaf node. -- If the value of a key contains a dot delimiter (`.`), use `Array.prototype.split('.')`, string transformations and `JSON.parse()` to create an object, then `Object.assign()` to create the leaf node. -- Otherwise, add the appropriate key-value pair to the accumulator object. +- Iterate over the object keys using `Object.keys(obj)` and `Array.prototype.forEach()`. +- Split each key with a dot delimiter (`.`) using `Array.prototype.split('.')`and use `Array.prototype.reduce()` to convert flattened paths to leaf node. +- If the current accumulator already contains value against a particular key, return its value as the next accumulator. +- Otherwise, add the appropriate key-value pair to the accumulator object and return value as the accumulator. + ```js -const unflattenObject = obj => - Object.keys(obj).reduce((acc, k) => { - if (k.indexOf('.') !== -1) { - const keys = k.split('.'); - Object.assign( - acc, - JSON.parse( - '{' + - keys.map((v, i) => (i !== keys.length - 1 ? `"${v}":{` : `"${v}":`)).join('') + - obj[k] + - '}'.repeat(keys.length) - ) - ); - } else acc[k] = obj[k]; - return acc; - }, {}); +const unflattenObject = obj =>{ + var result = {} + Object.keys(obj).forEach((k)=>{ + var keys = k.split('.') + keys.reduce(function(acc, e, i) { + return acc[e] || (acc[e] = isNaN(Number(keys[i + 1])) ? (keys.length - 1 === i ? obj[k] : {}) : []) + }, result) + }) + return result +} ``` ```js unflattenObject({ 'a.b.c': 1, d: 1 }); // { a: { b: { c: 1 } }, d: 1 } +unflattenObject({ 'a.b': 1, 'a.c': 2, d: 3 }); // { a: { b: 1, c: 2 }, d: 3 } +unflattenObject({ 'a.b.0': 8, d: 3 }) //{ a: { b: [ 8 ] }, d: 3 } ``` From cda0b131d9ea8202b5455ce99ae61b847f86385c Mon Sep 17 00:00:00 2001 From: taimoor Date: Tue, 22 Sep 2020 17:27:55 +0500 Subject: [PATCH 2/5] cater change request --- snippets/unflattenObject.md | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/snippets/unflattenObject.md b/snippets/unflattenObject.md index a2e757ac9..d3cd0710c 100644 --- a/snippets/unflattenObject.md +++ b/snippets/unflattenObject.md @@ -9,18 +9,14 @@ Unflatten an object with the paths for keys. - Split each key with a dot delimiter (`.`) using `Array.prototype.split('.')`and use `Array.prototype.reduce()` to convert flattened paths to leaf node. - If the current accumulator already contains value against a particular key, return its value as the next accumulator. - Otherwise, add the appropriate key-value pair to the accumulator object and return value as the accumulator. - - ```js const unflattenObject = obj =>{ - var result = {} - Object.keys(obj).forEach((k)=>{ - var keys = k.split('.') - keys.reduce(function(acc, e, i) { - return acc[e] || (acc[e] = isNaN(Number(keys[i + 1])) ? (keys.length - 1 === i ? obj[k] : {}) : []) - }, result) - }) - return result + return Object.keys(obj).reduce((acc1 , k)=>{ + k.split('.').reduce(function(acc, e, i , keys) { + return acc[e] || (acc[e] = isNaN(Number(keys[i + 1])) ? (keys.length - 1 === i ? obj[k] : {}) : []); + }, acc1) + return acc1; + },{}); } ``` From f3ddb653f0165ec4ac4b870636a799faa4cfc362 Mon Sep 17 00:00:00 2001 From: taimoor Date: Tue, 22 Sep 2020 17:31:40 +0500 Subject: [PATCH 3/5] update description --- snippets/unflattenObject.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/unflattenObject.md b/snippets/unflattenObject.md index d3cd0710c..c235eba4c 100644 --- a/snippets/unflattenObject.md +++ b/snippets/unflattenObject.md @@ -5,8 +5,8 @@ tags: object,advanced Unflatten an object with the paths for keys. -- Iterate over the object keys using `Object.keys(obj)` and `Array.prototype.forEach()`. -- Split each key with a dot delimiter (`.`) using `Array.prototype.split('.')`and use `Array.prototype.reduce()` to convert flattened paths to leaf node. +- Use nested `Array.prototype.reduce()` to convert the flat path to a leaf node. +- Split each key with a dot delimiter (`.`) using `Array.prototype.split('.')`and use `Array.prototype.reduce()` to add or objects against the keys. - If the current accumulator already contains value against a particular key, return its value as the next accumulator. - Otherwise, add the appropriate key-value pair to the accumulator object and return value as the accumulator. ```js From 351ce48f645efe85108812c691f99e2f3b9800a1 Mon Sep 17 00:00:00 2001 From: taimoor Date: Tue, 22 Sep 2020 17:37:22 +0500 Subject: [PATCH 4/5] update name for external accumulator. --- snippets/unflattenObject.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/unflattenObject.md b/snippets/unflattenObject.md index c235eba4c..a030d7f9d 100644 --- a/snippets/unflattenObject.md +++ b/snippets/unflattenObject.md @@ -11,11 +11,11 @@ Unflatten an object with the paths for keys. - Otherwise, add the appropriate key-value pair to the accumulator object and return value as the accumulator. ```js const unflattenObject = obj =>{ - return Object.keys(obj).reduce((acc1 , k)=>{ + return Object.keys(obj).reduce((res , k)=>{ k.split('.').reduce(function(acc, e, i , keys) { return acc[e] || (acc[e] = isNaN(Number(keys[i + 1])) ? (keys.length - 1 === i ? obj[k] : {}) : []); - }, acc1) - return acc1; + }, res) + return res; },{}); } ``` From c86c2c8744cdbf56f2a41234a5fcea9a4ba083b8 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 22 Sep 2020 22:37:05 +0300 Subject: [PATCH 5/5] Update unflattenObject.md --- snippets/unflattenObject.md | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/snippets/unflattenObject.md b/snippets/unflattenObject.md index a030d7f9d..bbe11292c 100644 --- a/snippets/unflattenObject.md +++ b/snippets/unflattenObject.md @@ -6,18 +6,25 @@ tags: object,advanced Unflatten an object with the paths for keys. - Use nested `Array.prototype.reduce()` to convert the flat path to a leaf node. -- Split each key with a dot delimiter (`.`) using `Array.prototype.split('.')`and use `Array.prototype.reduce()` to add or objects against the keys. -- If the current accumulator already contains value against a particular key, return its value as the next accumulator. -- Otherwise, add the appropriate key-value pair to the accumulator object and return value as the accumulator. +- Use `String.prototype.split('.')` to split each key with a dot delimiter and `Array.prototype.reduce()` to add objects against the keys. +- If the current accumulator already contains a value against a particular key, return its value as the next accumulator. +- Otherwise, add the appropriate key-value pair to the accumulator object and return the value as the accumulator. + ```js -const unflattenObject = obj =>{ - return Object.keys(obj).reduce((res , k)=>{ - k.split('.').reduce(function(acc, e, i , keys) { - return acc[e] || (acc[e] = isNaN(Number(keys[i + 1])) ? (keys.length - 1 === i ? obj[k] : {}) : []); - }, res) - return res; - },{}); -} +const unflattenObject = obj => + Object.keys(obj).reduce((res, k) => { + k.split('.').reduce( + (acc, e, i, keys) => + acc[e] || + (acc[e] = isNaN(Number(keys[i + 1])) + ? keys.length - 1 === i + ? obj[k] + : {} + : []), + res + ); + return res; + }, {}); ``` ```js