Created
July 11, 2017 11:40
-
-
Save aliaspooryorik/f285e910da7d17f229ec8493901a788c to your computer and use it in GitHub Desktop.
Flatten complex data
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
<cfscript> | |
struct = { | |
"a": 1, | |
"b": 2, | |
"c": 3, | |
"d": { | |
"e": 4 | |
} | |
}; | |
array = [1,[2,[3],4],5]; | |
function flattenStruct(data) { | |
return data.reduce(function(result, key, value) { | |
if (isStruct(value)) { | |
result.append(flattenStruct(value)); | |
} else { | |
result[key] = value; | |
} | |
return result; | |
}, {}); | |
} | |
writeDump(struct); | |
writeDump(flattenStruct(struct)); | |
function flattenArray(data) { | |
return data.reduce(function(result, item) { | |
if (isArray(item)) { | |
result.append(flattenArray(item), true); | |
} else { | |
result.append(item); | |
} | |
return result; | |
}, []); | |
} | |
writeDump(array); | |
writeDump(flattenArray(array)); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment