js笔记

1.克隆对象

克隆数组:

var country=['中国','美国'];
var copyCountry=country.slice(0);

克隆对象:

var people={sex:'man',age:4};
var me=JSON.parse(JSON.stringify(people));

2.随机数

从数组中随机取n个不重复的元素

 function getRandomArrayElements(arr, count) {
        var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
        while (i-- > min) {
            index = Math.floor((i + 1) * Math.random());
            temp = shuffled[index];
            shuffled[index] = shuffled[i];
            shuffled[i] = temp;
        }
        return shuffled.slice(min);
    }

 3.回车代替点击

    <button id="btnStart">开始</button>
<script>
    $(function () {     
        $("body").keypress(function (e) {      
            if (e.which == 13)
            {
                $("#btnStart").focus().click();
            }
        });

        $("#btnStart").click(function () {
            alert("我被点击了");
        });
    });

 4.数组倒序

var a, l;  
a = new Array(0,1,2,3,4);  
l = a.reverse();

5.获取客户端时间标准格式

function CurentTime()
    { 
        var now = new Date();
       
        var year = now.getFullYear();       //
        var month = now.getMonth() + 1;     //
        var day = now.getDate();            //
       
        var hh = now.getHours();            //
        var mm = now.getMinutes();          //
        var ss = now.getSeconds();           //
       
        var clock = year + "-";
       
        if(month < 10)
            clock += "0";
       
        clock += month + "-";
       
        if(day < 10)
            clock += "0";
           
        clock += day + " ";
       
        if(hh < 10)
            clock += "0";
           
        clock += hh + ":";
        if (mm < 10) clock += '0'; 
        clock += mm+":"; 
        
        if(ss<10) clock+='0';
        clock+=ss;
        
        return(clock); 
    } 

格式为:yyyy-MM-dd HH:mm:ss

6.获取Url参数

function tb_parseQuery(query) {
var Params = {};
if (!query) { return Params; }// return empty object
var Pairs = query.split(/[;&]/);
for (var i = 0; i < Pairs.length; i++) {
var KeyVal = Pairs[i].split('=');
if (!KeyVal || KeyVal.length != 2) { continue; }
var key = unescape(KeyVal[0]);
var val = unescape(KeyVal[1]);
val = val.replace(/\+/g, ' ');
Params[key] = val;
}
return Params;
}

 

 

 

var getJsonObj = tb_parseQuery(location.href);

getJsonObj.out_trade_no

 7.将数字类型保留2位小数

function changeTwoDecimal_f(x) { 
var f_x = parseFloat(x); 
if (isNaN(f_x)) { 
alert('function:changeTwoDecimal->parameter error'); 
return false; 
} 
f_x = Math.round(f_x * 100) / 100; 
var s_x = f_x.toString(); 
var pos_decimal = s_x.indexOf('.'); 
if (pos_decimal < 0) { 
pos_decimal = s_x.length; 
s_x += '.'; 
} 
while (s_x.length <= pos_decimal + 2) { 
s_x += '0'; 
} 
return s_x; 
} 

 8.数组去除重复

var unique = function(arr) {
var result = [], json = {};
for (var i = 0, len = arr.length; i < len; i++){
if (!json[arr[i]]) {
json[arr[i]] = 1;
result.push(arr[i]); //返回没被删除的元素
}
}
return result;
};

 9.字符串按照字符拆分放进数组

var a=Array.prototype.join.call('hello', '-');
var b=a.split("-")

 10.从一个数组里面随机取N个元素

  ///从一个数组中随机取n个元素
    function getRandomArrayElements(arr, count) {
        var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
        while (i-- > min) {
            index = Math.floor((i + 1) * Math.random());
            temp = shuffled[index];
            shuffled[index] = shuffled[i];
            shuffled[i] = temp;
        }
        return shuffled.slice(min);
    }

 11.将一个数组随机排序

 function arrayRandom() {
                array = array.length ? array : array1;
                var l = array.length;
                array1 = [];
                while (l) {
                    var rd = parseInt(Math.random() * l);
                    temp = array[rd];
                    array[rd] = array[l - 1];
                    array[l - 1] = temp;
                    array1.push(temp);
                    array.pop();
                    l -= 1;
                }
                array = array1;
            }

 

posted on 2016-12-13 17:32  花生哒哒  阅读(210)  评论(0编辑  收藏  举报

导航