niuben
1/27/2015 - 11:14 AM

JavaScript 常用操作

JavaScript 常用操作

/*
	{
		data: [{
			comment: "默认模板",
			groupSerDetails: [{
				aclDetailInfos: [{
					charge: false,
					customized: false,
					enable: true,
					name: "tips",
					nameCn: "输入提示",
					serAcls: [{
						aclId: 9,
						aclRemark: "",
						current: true
					}],
					serviceRemark: "http://qinputtip.amap.com//sisserver.php?query_type=inputtip&data_type=poi%7cbus%7cbusline http://10.137.84.36:3028",
					sid: 9,
					sidSwith: true,
					urlMappings: "/assistant/inputtips"
				}, {
					charge: false,
					customized: false,
					enable: true,
					name: "cost",
					nameCn: "路程所耗费用",
					serAcls: [{
						aclId: 18,
						aclRemark: "",
						current: false
					}],
					serviceRemark: "http://qcost.amap.com:8080/Cost/CostEngine? 青岛 http://10.17.128.133:8080/Cost/CostEngine? functest 10.101.12.213:8080",
					sid: 19,
					sidSwith: false,
					urlMappings: "/cost"
				}],
				gname: "",
				gsid: 0			
			}]
		}]
	}
	后端同学给出这样一个数据类型,我需要将gname,gsid两个属性值,放到aclDetailInfos数组里面每个对象中;

	obj:      Object 基本对象;
	rootName: String 需要遍历的对象属性。有三种格式
			  1. 单个属性值。例如: ”data“,是指obj["data"]
			  2. 多个属性值。例如: "data.list", 是指obj["data"]["list"]
			  3. 多个属性值和数字。例如:"data.0.list", 是指obj["data"][0]["list"]
			  多个属性值和数字之间只能用”."连接
    loopName: String 想要获得的对象列表名称 

*/


function outerToInner(obj, rootName, loopName) { //通过rootName知道根对象,通过loopName知道循环对象就是我们需要提出来的对象
	var list = [],
		rootArr = obj,
		loopArr = null;

	//找到最外层数据对象

	var rootNameArr = rootName.split(".");
	rootNameArr.map((name, index) => {
		if (rootArr[name] != undefined) {
			rootArr = rootArr[name];
		}
	});

	rootArr.map((item) => {
		var subLoopArr = [];
		item[loopName].map((sItem, sIndex) => {
			//不是数组第一个直接返回所有值;
			if (sIndex != 0) {
				list.push(sItem)
				return sItem;
			}

			//是数组第一个需要复制外层属性并增加多少列;
			for (var name in item) {
				if (name == loopName) {
					continue;
				}

				//如果外层属性值和内层属性值相等,为了防止属性值被覆盖,给外层属性值加上前缀
				var prevName = sItem[name] == undefined ? "" : "f_";
				sItem[prevName + name] = item[name];
			}
			sItem.cols = item[loopName].length;
			list.push(sItem);
		})
	});
	return list;
}
/*
 
*/
var parseUrl = function(url){
	var obj = {},
		arr = [];

	var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");	
	for(var i in paraString){
		arr = paraString[i].split("=");
		obj[arr[0]] = arr[1]; 
	} 
	
	return obj;
}
/*
                给循环设置样式创建一个统一函数,
                @elements 需要绑定的元素组
                @callback 点击设置完成回调函数,并返回当前位置
                @className 设置的样式名称(可选),如果没有的话,默认为selected
            */
            var eachStyle = function(elements, callback, className) {
                className = className || "selected";

                $(elements).each(function(index){
                    $(this).click(function(){                
                        $(elements).removeClass(className);
                        $(this).addClass(className);

                        callback && callback(index);
                    });
                });
            };
function isArray(param) { 
  if (Object.prototype.toString.call(param) === '[object Array]') { 
    return true; 
  }
  return false;
} 
/* 基础类 */
	var Base = {
		id: "",
		events: {

		},
		eventInit: function(){
			/* 初始化events对象 */
			var events = this.events,
			that = this;
			for(var name in events) {
				(function(name){
					var func = events[name];
					
					if(name.indexOf(" ") == -1 ) {
						return false;
					};

					var nameArray = name.split(" "),
					eventName = nameArray.shift(),
					selector = nameArray.join(" "),
					func = events[name];

					$("#" + that.id).on(eventName, selector, function(e){
						 that[events[name]] && that[events[name]](); 
					})
				})(name);
			}
		}
	};