Created
June 15, 2017 12:27
-
-
Save zetxx/53f0450913c55d3b56f8f3dca4dbbcc5 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/judinuqaso
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// flattenAPI(data) | |
'use strict'; | |
function flatten(data, path) { | |
path = path || []; | |
if (!(data instanceof Array) && data instanceof Object) { | |
return Object.keys(data).reduce(function (acumu, cur) { | |
var c = path.concat(cur).join('.'); | |
var d = data[cur]; | |
if (!(d instanceof Array) && d instanceof Object) { | |
return Object.assign(acumu, abc(d, [c])); | |
} | |
acumu[c] = d; | |
return acumu; | |
}, {}); | |
} | |
return data; | |
} | |
console.log(flatten({ | |
a1: { | |
bb2: { c3: 3 } | |
}, | |
aa1: { bb3: [1, 2] }, | |
b1: { d2: 1, dd2: 2, ddd2: { aaa3: 1 } } | |
})); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// flattenAPI(data) | |
function flatten(data, path){ | |
path = path || []; | |
if(!(data instanceof Array) && (data instanceof Object)) { | |
return Object.keys(data).reduce((acumu, cur) => { | |
let c = path.concat(cur).join('.'); | |
let d = data[cur]; | |
if(!(d instanceof Array) && (d instanceof Object)) { | |
return Object.assign(acumu, abc(d, [c])); | |
} | |
acumu[c] = d; | |
return acumu; | |
}, {}); | |
} | |
return data; | |
} | |
console.log(flatten( | |
{ | |
a1: { | |
bb2: {c3: 3} | |
}, | |
aa1: {bb3: [1, 2]}, | |
b1: {d2: 1, dd2: 2, ddd2: {aaa3: 1}} | |
} | |
));</script></body> | |
</html> |
This file contains 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
// flattenAPI(data) | |
'use strict'; | |
function flatten(data, path) { | |
path = path || []; | |
if (!(data instanceof Array) && data instanceof Object) { | |
return Object.keys(data).reduce(function (acumu, cur) { | |
var c = path.concat(cur).join('.'); | |
var d = data[cur]; | |
if (!(d instanceof Array) && d instanceof Object) { | |
return Object.assign(acumu, abc(d, [c])); | |
} | |
acumu[c] = d; | |
return acumu; | |
}, {}); | |
} | |
return data; | |
} | |
console.log(flatten({ | |
a1: { | |
bb2: { c3: 3 } | |
}, | |
aa1: { bb3: [1, 2] }, | |
b1: { d2: 1, dd2: 2, ddd2: { aaa3: 1 } } | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment