编程高手和菜鸟的区别

  • Post author:
  • Post category:其他


1.计算系统当前是星期几

菜鸟:

var str = “”;

var week = new Date().getDay();

if (week === 0) {


str = “今天是星期日”;

} else if (week === 1) {


str = “今天是星期一”;

} else if (week === 2) {


str = “今天是星期二”;

} else if (week === 3) {


str = “今天是星期三”;

} else if (week === 4) {


str = “今天是星期四”;

} else if (week === 5) {


str = “今天是星期五”;

} else if (week === 6) {


str = “今天是星期六”;

}

中层:

var a = [“日”, “一”, “二”, “三”, “四”, “五”, “六”];

var week = new Date().getDay();

var str = “今天是星期” + a[week];

console.log(str);

高手:

var str = “今天是星期” + “日一二三四五六”.charAt(new Date().getDay());

2.数组对象某一属性映射

菜鸟:

var a1 =[{


id: “0001”,

placeCode: ‘sh’,

value: 0.5

},{


id: “0002”,

placeCode: ‘sz’,

value: 1.5

},{


id: “0003”,

placeCode: ‘sh’,

value: 2.5

}];

    var newValue;
    var a2 = $.map(a1, function(obj,key){
        if (obj.placeCode == "sh") {
            newValue = "沪";
        } else  if (obj.placeCode == "sz"){
            newValue = "深";
        }
        return ($.extend(obj,{value:newValue}));
    });

中层:

var maps= new Map([[‘sh’,’沪’],[‘sz’,’深’]]);

var a2 = $.map(a1, function(obj,key){


obj.placeCode = maps.get(obj.placeCode);

return obj;

});

高手:

var toStr = {


sh : ‘沪’,

sz : ‘深’

};

var a2 = $.map(a1, function(obj,key){


var placeCode = obj.placeCode;

obj.placeCode = toStr[placeCode] ? toStr[placeCode] : placeCode;

return obj;

});

转载于:https://blog.51cto.com/13968096/2362989