I am dealing with a scenario where I get data in the following form:
var data = {
'user.name': 'renatoargh',
'user.age': 27,
'user.interests.0.description': 'coding',
'user.interests.0.receiveNotifications': true,
'user.interests.1.description': 'bungee jumping',
'user.interests.1.receiveNotifications': false
};
So in order to validate and persist this data I decided to create a way to unflat these strings into regular JS objects, and send them to MongoDb (since it is schema-less).
I coded an algorithm that I am unsure that works for most cases so I created a fiddle that seems to be able to deal with an extreme case, full of nested arrays.
Can someone point any week point or case in which my algorithm would fail? Any kind of advice will be appreciated.
Link to the fiddle here!
Heres the code, where messageObject
is the array and the output is in messageTree
function forEachOwnProperty(object, iterator) {
for(var property in object){
if(object.hasOwnProperty(property)) {
iterator(property, object[property]);
}
}
}
forEachOwnProperty(messageObject, function(property, messages) {
property = property.split('.');
var currentNode = messageTree;
for(var i = 0; i < property.length; i++) {
var currentProperty = property[i];
if(typeof currentNode[currentProperty] === 'undefined') {
if(i === property.length - 1) {
currentNode[currentProperty] = messages
} else {
if(/^\+?(0|[1-9]\d*)$/.test(property[i + 1])) {
currentNode[currentProperty] = [];
} else {
currentNode[currentProperty] = {};
}
}
}
currentNode = currentNode[currentProperty];
}
});