JSFiddle - React, Tailwind, and code Playground

by JosephGarrone

JavaScript

var data1 = {
  "[email protected]": {
    "[email protected]": {
      "[email protected]": {
        "[email protected]": {
          "[email protected]": {}
        },
        "[email protected]": {}
      },
      "[email protected]": {
      "[email protected]": {}
      }
    },
    "[email protected]": {}
  }
};

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;
}