cater change request

This commit is contained in:
taimoor
2020-09-22 17:27:55 +05:00
parent 3bcce60510
commit cda0b131d9

View File

@ -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. - 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. - 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. - Otherwise, add the appropriate key-value pair to the accumulator object and return value as the accumulator.
```js ```js
const unflattenObject = obj =>{ const unflattenObject = obj =>{
var result = {} return Object.keys(obj).reduce((acc1 , k)=>{
Object.keys(obj).forEach((k)=>{ k.split('.').reduce(function(acc, e, i , keys) {
var keys = k.split('.') return acc[e] || (acc[e] = isNaN(Number(keys[i + 1])) ? (keys.length - 1 === i ? obj[k] : {}) : []);
keys.reduce(function(acc, e, i) { }, acc1)
return acc[e] || (acc[e] = isNaN(Number(keys[i + 1])) ? (keys.length - 1 === i ? obj[k] : {}) : []) return acc1;
}, result) },{});
})
return result
} }
``` ```