Skip to content

Instantly share code, notes, and snippets.

@kimyongin
Last active September 27, 2021 07:14
Show Gist options
  • Save kimyongin/4993c23701d3cf8759a67f720a6655dd to your computer and use it in GitHub Desktop.
Save kimyongin/4993c23701d3cf8759a67f720a6655dd to your computer and use it in GitHub Desktop.
var data = {
header: {
"x-request-id": "1"
},
"body": {
"guid": "1234",
"items": [
{
"name": "item1",
"quantity": {"num": [{value: 100, data: "A"}, {value: 101, data: "A1"}], "numStr": "hundread"}
},
{
"name": "item2",
"quantity": {"num": [{value: 200, data: "B"}, {value: 201, data: "B1"}], "numStr": "two hundread"}
}
]
}
}
function myFlatten(target, opt = {}) {
const delimiter = '.'
const output = {}
if (opt.allKey){
output[opt.allKey] = []
}
function step(object, prev, hasArray) {
const rootType = Object.prototype.toString.call(object)
const rootIsArray = (rootType === '[object Array]')
Object.keys(object).forEach(function (key) {
const value = object[key]
const type = Object.prototype.toString.call(value)
const isObject = (type === '[object Object]')
const isArray = (type === '[object Array]')
let newKey = opt.arrayFlatten && rootIsArray ? "" : delimiter + key
newKey = prev ? prev + newKey : key
if ((isObject || isArray) && Object.keys(value).length) {
return step(value, newKey, hasArray || rootIsArray || isArray)
}
const strValue = value.toString()
opt.allKey && output[opt.allKey].push(strValue)
if (opt.arrayFlatten && hasArray) {
if (!output.hasOwnProperty(newKey)) {
output[newKey] = []
}
output[newKey].push(strValue)
} else {
output[newKey] = strValue
}
})
}
step(target)
return output
}
console.log(myFlatten(data, {allKey : "all", arrayFlatten : false}));
/*
{
all: [
'1', '1234',
'item1', '100',
'A', '101',
'A1', 'hundread',
'item2', '200',
'B', '201',
'B1', 'two hundread'
],
'header.x-request-id': '1',
'body.guid': '1234',
'body.items.0.name': 'item1',
'body.items.0.quantity.num.0.value': '100',
'body.items.0.quantity.num.0.data': 'A',
'body.items.0.quantity.num.1.value': '101',
'body.items.0.quantity.num.1.data': 'A1',
'body.items.0.quantity.numStr': 'hundread',
'body.items.1.name': 'item2',
'body.items.1.quantity.num.0.value': '200',
'body.items.1.quantity.num.0.data': 'B',
'body.items.1.quantity.num.1.value': '201',
'body.items.1.quantity.num.1.data': 'B1',
'body.items.1.quantity.numStr': 'two hundread'
}
*/
console.log(myFlatten(data, {allKey : "all", arrayFlatten : true}));
/*
{
all: [
'1', '1234',
'item1', '100',
'A', '101',
'A1', 'hundread',
'item2', '200',
'B', '201',
'B1', 'two hundread'
],
'header.x-request-id': '1',
'body.guid': '1234',
'body.items.name': [ 'item1', 'item2' ],
'body.items.quantity.num.value': [ '100', '101', '200', '201' ],
'body.items.quantity.num.data': [ 'A', 'A1', 'B', 'B1' ],
'body.items.quantity.numStr': [ 'hundread', 'two hundread' ]
}
*/
@kimyongin
Copy link
Author

kimyongin commented Sep 22, 2021

기본 구조는 아래 라이브러리의 flatten 을 참고했음
https://github.com/hughsk/flat/blob/master/index.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment