Js recursively converts package names to Corresponding Hierarchical objects c
Example: The original object {a: {test: 1, B: 2} is based on. b. c. d To: {a: {test: 1, B: {c: {d :{}}}}}
Note: The package name is available. the json object itself is composed of attributes and values and can be nested. Therefore, there is a requirement to convert the package description string to the json type, note other existing attributes. in this example, the value of attribute a is an object. Therefore, you only need to extend the content of attribute B, and the value of attribute B is not an object type. Therefore, the object must be overwritten, other non-existent attributes should be paid to empty objects.
This example uses recursion:
Function namespace (oNamespace, sPackage) {var obj = oNamespace; var pkg =; if (sPackage. indexOf (.) =-1) {pkg = sPackage;} else {pkg = sPackage. substr (0, 1);} if (pkg &&! (Obj [pkg] & obj [pkg] instanceof Object) {obj [pkg] ={};} if (sPackage. indexOf (.)! =-1) {namespace (obj [pkg], sPackage. substr (2);} return obj ;}
Test code:
Var obj = namespace ({a: {test: 1, B: 2 }}, 'a. b. c. d'); console. log (JSON. stringify (obj ));
Result:
{A: {test: 1, B: {c: {d :{}}}}}