• 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;
                }
  • 相关阅读:

    卖了5个月水果之后再看互联网思维
    实时流式计算框架Storm 0.9.0发布通知(中文版)
    Top100Summit全球案例研究峰会第一天总结——云计算和大数据
    [若有所悟]提升工作效率的一些小技巧——资源管理器篇
    [若有所悟]主持会议的八大戒条
    惊魂36小时,一次生产事故,动态磁盘删除卷分区丢失,数据恢复案例实战
    一个不用的域名到期导致整个服务器上其他网站都无法访问的线上事故
    guava cache大量的WARN日志的问题分析
    IDEA下运行 mybatis报错 Parameter 'arg0' not found. Available parameters are [autoRecharge, id, param1, param2]
  • 原文地址:https://www.cnblogs.com/CallmeYhz/p/6170815.html
Copyright © 2020-2023  润新知