Skip to content

Instantly share code, notes, and snippets.

@tomasdev
Created November 25, 2015 00:27
Show Gist options
  • Save tomasdev/5d193d14d2aefffecf6c to your computer and use it in GitHub Desktop.
Save tomasdev/5d193d14d2aefffecf6c to your computer and use it in GitHub Desktop.
Convert dot notation with subs [n] to object-array-like
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