From the rear end of the recursive parameter bound to a higher portability

Under the front and rear end of the separation, the relevant interface definitions, parameters change, there is always a back-end data restructuring idea untold. The same person multiple back-end docking interfaces, reducing the relative quiet because parameter modification, which led to the front along with the changes.

At present, priority, from the shackles of the rear end of the parameter name, so that the front end of a free-defined parameters, and unwanted discharge parameters defined in the rear end. The method is simple, directly on the method. The principle is recursively traverse, according to the map object mapping parameters.

The main functions:

var dataval = {}; // backend parameter object
var parent = {}; // new object storage vessel
var mapval = {// map mapping parameters
    "MSG": "webmsg"  
    
}

function mapName(dataval, mapval, parent){ //原对象数据  地图  新对象数据
            var dataval = dataval;
            var mapval = mapval;
            var parent = parent;
            
            for(var attr in dataval){
                if(!mapval[attr]){continue;}
                var typeData = typeof dataval[attr];
                
                if(typeData == "object"){
                      parent[mapval[attr]] = {};
                      if(dataval[attr] instanceof Array){
                            parent[mapval[attr]] = [];
                            for(var i =0; i<dataval[attr].length; i++){
                                var ms = {};
                                parent[mapval[attr]].push(ms);
                                mapName(dataval[attr][i], mapval, parent[mapval[attr]][i]);
                            }
                            
                        }else{
                              mapName(dataval[attr], mapval, parent[mapval[attr]]);
                        }
                    
                }else{
                    parent[mapval[attr]] = dataval[attr];
                }
            }
        }
  }

 

Guess you like

Origin blog.csdn.net/guanzhangl/article/details/89508597