Const FS = require (' FS ');//---------------Read the source file---------------Const ORIGINDATA = require ('./vuxarea3.json '));//---------------constant---------------CONST CONSTANT ={array_type:' Array ', Json_type:' Json ',};//configuration of the---------------transformation---------------Const CONFIG={originkey: {children:NULL, Parent:' Parent ', Value:' Value ', Label:' Name '}, Origintype:constant. Array_type, Targetkey: {children:' Children ', Parent:NULL, Value:' Value ', Label:' Label '}, Targettype:constant. Json_type};//---------------conversion function---------------functionConvertarraytojson (InitData, {originkey, targetkey}) {if(!initdata | | initdata.length = = = 0)return []; if(!initdata.length = = 1)returnInitData; if(! (InitDatainstanceofArray))return[InitData]; Const result= []; Const TEMPMAP= []; Const Key=Originkey.value; Const Parentkey=originkey.parent; Const L=initdata.length; Let I; //Key ConversionConst DATA = initdata.map (item =({[originkey.parent]: item[originkey.parent], [Targetkey.value]: Item[originkey.value], [Targetkey.label]: Item[originkey.label])); //Temporary References for(i = 0; I < L + + + 1) {Tempmap[data[i][key]]=Data[i]; } //leaves are mounted by temporary references, and all nodes are singleton, so they can be used to mount the leaves by temporary reference . //root element push directly to the root node for(i = 0; I < L + + + 1) {Const Parentvalue=Data[i][parentkey]; //the current element has a parent element, and the child is not equal to the parent if(Tempmap[parentvalue] && Data[i][key]!==parentvalue) { //The current element is added to the children on the parent elementTempmap[parentvalue][targetkey.children] =Tempmap[parentvalue][targetkey.children]|| []; Tempmap[parentvalue][targetkey.children].push (Data[i]); } Else { //the current element is the root elementResult.push (Data[i]); } } returnresult;}functionCONVERT (data, {originkey, Origintype, Targetkey, TargetType}) {if(Origintype = = = constant. Array_type && TargetType = = =constant. Json_type) {returnConvertarraytojson (data, {originkey, targetkey}); } return [];}//---------------Execution---------------Const RESULT =convert (origindata, config); let ResultStr= ' ';Try{resultstr=json.stringify (result);} Catch(e) {console.log (e);} ResultStr= Resultstr.replace (/"Parent": "[a-za-z\d]+",/g, ");//---------------Write---------------Fs.writefile ('./result.json ', ResultStr, ' Utf-8 ', (err) = { if(Err) {Console.log (err); } Else{Console.log (' # # # Conver ${config.origintype} to ${config.targettype} success! `); }});
ConstFs= require(' FS ');
//---------------Read the source file---------------ConstOrigindata= require('./vuxarea3.json ');
//---------------constant---------------Constconstant={array_type:' Array ', Json_type:' Json ',};
//---------------The configuration of the transformation---------------
ConstConfig={originkey: {children:NULL, Parent:' Parent ', Value:' value ', Label:' name '}, Origintype:constant. Array_type, Targetkey: {children:' Children ', Parent:NULL, Value:' value ', Label:' label '}, Targettype:constant. Json_type};
//---------------conversion function---------------function Convertarraytojson(InitData, {Originkey,Targetkey}) {if(!InitData||Initdata.length=== 0)return[];if(!Initdata.length=== 1)returnInitData;if(!(InitDatainstanceof Array))return[InitData];
ConstResult=[];ConstTempmap=[];
ConstKey=Originkey.value;ConstParentkey=Originkey.parent;ConstL=Initdata.length; LetI
//Key conversion ConstData=InitData.Map(Item =({[originkey.parent]: item[originkey.parent], [Targetkey.value]: Item[originkey.value], [Targetkey.label]: item[ Originkey.label]));
//Temporary reference for(I= 0; I<L I+= 1) {Tempmap[data[i][key]]=Data[i]; }
//leaves are mounted by temporary reference, and all nodes are singleton, which can be used to mount the leaves by temporary reference . //root element push directly to the root node for(I= 0; I<L I+= 1) {ConstParentvalue=Data[i][parentkey];the current element has a parent element, and the child is not equal to the parent if(Tempmap[parentvalue]&&Data[i][key]!==Parentvalue) {The current element is added to the children on the parent elementTempmap[parentvalue][targetkey.children]=Tempmap[parentvalue][targetkey.children]||[]; Tempmap[parentvalue][targetkey.children].Push(Data[i]); }Else{//The current element is the root elementResult.Push(Data[i]); } }returnResult;}
function Convert(Data, {Originkey,Origintype,Targetkey,TargetType}) {if(Origintype===constant. Array_type&&TargetType===constant. Json_type) {return Convertarraytojson(data, {originkey, targetkey}); }return[];}
//---------------execution---------------ConstResult= Convert(origindata, config); LetResultStr= "';Try{ResultStr= JSON.stringify(result);}Catch(e) {Console.Log(e);} ResultStr=ResultStr.Replace(/"Parent": "[a-za-z\d]+",/g,"');
//---------------Write---------------Fs.WriteFile('./result.json ', ResultStr,' Utf-8 ', (Err)={if(ERR) {Console.Log(err); }Else{Console.Log(' # # Conver${Config.origintype} to${Config.targettype}success! '); }});
"Notes" Nodejs read JSON, array to tree