Created
November 25, 2015 00:27
-
-
Save tomasdev/5d193d14d2aefffecf6c to your computer and use it in GitHub Desktop.
Convert dot notation with subs [n] to object-array-like
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
var test = {}; | |
for (var key in eventData) { | |
key.split('.').reduce(function (lastReturn, prop, index, arr) { | |
if (!lastReturn[prop]) { | |
if (arr.length - 1 === index) { | |
lastReturn[prop] = eventData[key]; | |
} else { | |
lastReturn[prop] = {}; | |
} | |
} | |
return lastReturn[prop]; | |
}, test); | |
} | |
for (var key in test) { | |
if (key.match(/\[\d+\]/)) { | |
var prefix = key.replace(/\[\d+\]/, ''); | |
test[prefix] = test[prefix] || []; | |
if (typeof test[key] === 'object') { | |
test[key] | |
} | |
test[prefix].push(test[key]); | |
delete test[key]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment