zoukankan      html  css  js  c++  java
  • js常用方法封装

    原文地址

    NPM地址 使用方法相同

    url参数提取

    //提取url中的参数
    function query(url) {
    	if(!(url instanceof String )) return {}
    	url = url.split('?')[1] //提取url问号?之后的字符串
    	if(!url){
    		return {}
    	}
    	url = url.split('&') //分离&之间的字符串参数
    	if(!url){
    		return {}
    	}
    	let params = {}
    	url.filter(item => {
    		item = item.toString().split('=')
    		let key = item[0]
    		let value = item[1]
    
    
    		params[key]= value
    		
    		
    	})
    	return params
    }
    //返回值为{key:value}
    

    深拷贝

    function cloneLoop(x) {
        //如果不是对象或者数组 直接返回
        if(!(x instanceof Object) || !(x instanceof Array) ) return x
    
        // 栈
        const loopList = [
            {
                parent: root,
                key: undefined,
                data: x,
            }
        ];
    
        while(loopList.length) {
            // 深度优先
            const node = loopList.pop();
            const parent = node.parent;
            const key = node.key;
            const data = node.data;
    
            // 初始化赋值目标,key为undefined则拷贝到父元素,否则拷贝到子元素
            let res = parent;
            if (typeof key !== 'undefined') {
                res = parent[key] = {};
            }
    
            for(let k in data) {
                if (data.hasOwnProperty(k)) {
                    if (typeof data[k] === 'object') {
                        // 下一次循环
                        loopList.push({
                            parent: res,
                            key: k,
                            data: data[k],
                        });
                    } else {
                        res[k] = data[k];
                    }
                }
            }
        }
    
        return root;
    }
    

    时间格式处理

    //主要用于处理相隔多少天的日期查询
    function dateFormat (subtract, isStart) { //时间差(天) //  是开始时间还是结束时间
        let nowDate = new Date().getTime() //今天的时间 1970 年 1 月 1 日至今的毫秒数
        let subDate = subtract * 24 * 60 * 60 * 1000 //计算减去所设置的时间
        let newDate = new Date(nowDate - subDate) //减去之后的时间
    
        let year = newDate.getFullYear() //年
        let month = newDate.getMonth() + 1 //月 之所以+1 是因为月份是0-11
        if (month < 10) {
            month = '0' + month
        }
        let day = newDate.getDate() //日 
        if (day < 10) {
            day = '0' + day
        }
        let HMS //小时分钟秒
        if (isStart) { //开始时间
            HMS = ' 00:00:00'
        } else {
            HMS = ' 23:59:59'
        }
        let date = year + '-' + month + '-' + day + HMS
        return date
    }
    

    sessionStorage的存取

    class Session {
        setSession(key, value) {
            window.sessionStorage.setItem(key, value)
        }
        getSession(key) {
            if (typeof key === 'string') {
                return window.sessionStorage.getItem(key) || ''
            }
            if (key instanceof Array) {
                const setKey = [...new Set(key)]
                let newObj = {}
                setKey.forEach(item => {
                    const value = window.sessionStorage.getItem(item) || ''
                    if (value) {
                        newObj[item] = value
                    }
                })
                return newObj
            }
        }
    }
    //get方法支持数组格式 如['key1','key2'] 返回值为{key1:key1,key2:key2}
    

    localStorage

    class Storage {
        setStorage(key, value) {
            window.localStorage.setItem(key, value)
        }
        getStorage(key) {
            if (typeof key === 'string') {
                return window.localStorage.getItem(key) || ''
            }
            if (key instanceof Array) {
                const setKey = [...new Set(key)]
                let newObj = {}
                setKey.forEach(item => {
                    const value = window.localStorage.getItem(item) || ''
                    if (value) {
                        newObj[item] = value
                    }
                })
                return newObj
            }
        }
    }
    //get方法支持字符串和数组
    //
    export default new Storage()
    

    快排

    function sort(arr) {
    	if (arr.length <= 1) {
    		return arr
    	}
    	const pivot = arr.splice(0, 1)[0]
    	const left = [],
    		right = []
    
    	arr.forEach((item, index) => {
    		if (item < pivot) {
    			left.push(item)
    		} else {
    			right.push(item)
    		}
    	})
    	// return arrSort(left).concat(pivot,arrSort(right))
    	return [...sort(left), pivot, ...sort(right)]
    }
    // 写着玩的 没啥用 理解原理就行
    
  • 相关阅读:
    C++计算器项目的初始部分
    视频教程自学计划
    1001.A+B Format (20)解题描述
    成为理想的自己
    Sample Join Analysis
    Sample MultipleFileWordcount CombineFileInputFormat
    FileOutputFormat
    Combine small files to Sequence file
    FileInputFormat
    Sample: Write And Read data from HDFS with java API
  • 原文地址:https://www.cnblogs.com/big0range/p/14204839.html
Copyright © 2011-2022 走看看