var data1 = {
"a@1.0.3": {
"b@2.3.0": {
"c@2.0.0": {
"d@0.4.0": {
"e@1.1.1": {}
},
"f@0.5.3": {}
},
"j@2.15.2": {
"x@1.2.3": {}
}
},
"i@1.5.8": {}
}
};
console.log(fixData(data1));
function fixData(data) {
var result = {};
for (var key in data) {
if (data.hasOwnProperty(key)) {
if (key.indexOf("@") == -1)
continue; // Ignore keys without @'s
var parts = key.split("@");
var left = parts[0];
var right = parts[1];
// Replace .'s with -'s
while (right.indexOf(".") > -1) {
right = right.replace(".", "-");
}
// Add up values
var num = 0;
for (var i = 0; i < right.length; i++) {
var char = right[i];
if (!isNaN(char)) {
num += parseInt(char);
}
}
left += num;
// Replace key
var existing = data[key];
result[left+"@"+right] = fixData(existing);
}
}
return result;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.