Last active
March 13, 2020 14:46
-
-
Save sempostma/babca19c3b986f489ba83f3a47162459 to your computer and use it in GitHub Desktop.
(Un)FlattenObject
This file contains hidden or 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
"use strict"; | |
var isArray = function(arg) { | |
return Object.prototype.toString.call(arg) === '[object Array]'; | |
}; | |
var isObject = function(arg) { | |
return Object.prototype.toString.call(arg) === '[object Object]' | |
} | |
var flattenObject = function flattenObject(objOrArray) { | |
var seperator = | |
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "_"; | |
if (isArray(objOrArray)) | |
return objOrArray.map(function(item) { | |
return flatten(item); | |
}); | |
var obj = objOrArray; | |
var map = {}; | |
var todoKeys = Object.keys(obj); | |
var todoValues = Object.values(obj); | |
while (todoKeys.length > 0) { | |
var tK = todoKeys.pop(); | |
var tV = todoValues.pop(); | |
if (isObject(tV)) { | |
var childKeys = Object.keys(tV); | |
for (var _i = 0, _childKeys = childKeys; _i < _childKeys.length; _i++) { | |
var key = _childKeys[_i]; | |
todoKeys.push(tK + seperator + key); | |
todoValues.push(tV[key]); | |
} | |
} else { | |
map[tK] = tV; | |
} | |
} | |
return map; | |
}; | |
var unFlattenObject = function unFlattenObject(objOrArray) { | |
var seperator = | |
arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "_"; | |
if (isArray(objOrArray)) | |
return objOrArray.map(function(item) { | |
return unFlattenObject(item); | |
}); | |
var obj = objOrArray; | |
var keys = Object.keys(obj); | |
var deep = {}; | |
for (var _i2 = 0, _keys = keys; _i2 < _keys.length; _i2++) { | |
var tK = _keys[_i2]; | |
var temp = deep; | |
var path = tK.split(seperator); | |
for (var i = 0; i < path.length - 1; i++) { | |
var key = path[i]; | |
temp = temp[key] = temp[key] || {}; | |
} | |
temp[path.pop()] = obj[tK]; | |
} | |
return deep; | |
}; |
This file contains hidden or 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
const flattenObject = (objOrArray, seperator = '_') => { | |
if (Array.isArray(objOrArray)) return objOrArray.map(item => flatten(item)) | |
const obj = objOrArray | |
const map = {} | |
const todoKeys = Object.keys(obj) | |
const todoValues = Object.values(obj) | |
while (todoKeys.length > 0) { | |
let tK = todoKeys.pop() | |
let tV = todoValues.pop() | |
if (typeof tV === 'object' && tV !== null) { | |
const childKeys = Object.keys(tV) | |
for (const key of childKeys) { | |
todoKeys.push(tK + seperator + key) | |
todoValues.push(tV[key]) | |
} | |
} else { | |
map[tK] = tV | |
} | |
} | |
return map | |
} | |
const unFlattenObject = (objOrArray, seperator = '_') => { | |
if (Array.isArray(objOrArray)) return objOrArray.map(item => unFlattenObject(item)) | |
const obj = objOrArray | |
const keys = Object.keys(obj) | |
const deep = {} | |
for (const tK of keys) { | |
let temp = deep | |
const path = tK.split(seperator) | |
for (let i = 0; i < path.length - 1; i++) { | |
const key = path[i] | |
temp = temp[key] = temp[key] || {} | |
} | |
temp[path.pop()] = obj[tK] | |
} | |
return deep | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment