Reputation: 3239
Hello guys I'm trying to parse an array of strings into a custom structure:
var str = [
"country.UK.level.1",
"country.UK.level.2",
"country.US.level.1",
"country.UK.level.3"
];
Into something like:
var ordered = {
"country": [
{"UK" : {"level" : ["1", "2", "3"]}},
{"US" : {"level" : ["1","2"]}}
]
}
Notes:
str
array will not be sorted and the code should be robust against that.x.y.x.y...
pattern, where x
will be unique for that array and y
can change. In my example country
and level
will always be the same as they represent the x
pos.str
array, can be of any length. The longer the string the deeper nesting.Upvotes: 0
Views: 661
Reputation: 386654
This solution utilized a Array.prototype.forEach
and Array.prototype.reduce
.
var str = [
"country.UK.level.1",
"country.UK.level.2",
"country.US.level.1",
"country.UK.level.3"
],
ordered = {};
str.forEach(function (a) {
var aa = a.split('.'),
val = aa.pop(),
last = aa.length - 1;
aa.reduce(function (obj, pro, i) {
if (!(pro in obj)) {
obj[pro] = i === last ? [] : {};
}
return obj[pro];
}, ordered).push(val);
});
document.write('<pre>' + JSON.stringify(ordered, 0, 4) + '</pre>');
Upvotes: 1
Reputation: 29836
This should work for you if the last level of your object is an array:
var str = [
"country.UK.level.1",
"country.UK.level.2",
"country.US.level.1",
"country.UK.level.3"
];
var obj = {};
str.forEach(function(str){
var curr = obj;
var splitted = str.split('.');
var last = splitted.pop();
var beforeLast = splitted.pop();
splitted.forEach(function(sub){
if(!curr.hasOwnProperty(sub))
{
curr[sub] = {};
}
curr = curr[sub];
});
if(!curr[beforeLast]){
curr[beforeLast] = [];
}
curr[beforeLast].push(last);
})
console.log(obj);
Upvotes: 1