js常用处理函数集合(慢慢增加)

一.数组

1.对象数组

(1)根据属性,把对应值组成新数组

var arr=[{
		"size": "4",
		"time": "2019-02-26 15:10:17"
	}, {
		"size": "4",
		"time": "2019-02-26 15:13:40"
	}, {
		"size": "4",
		"time": "2019-02-28 17:10:06"
	}, {
		"size": "4",
		"time": "2019-02-28 17:12:52"
	}, {
		"size": "4",
		"time": "2019-02-28 17:21:30"
	}, {
		"size": "4",
		"time": "2019-02-28 17:32:00"
	}, {
		"size": "4",
		"time": "2019-02-28 17:38:00"
	}, {
		"size": "4",
		"time": "2019-02-28 17:48:00"
	}, {
		"size": "4",
		"time": "2019-02-28 17:57:01"
	}]
	function doArr(arr,type){
		var newArr=[]
		for (let i in arr){
			newArr.push(arr[i][type])
		}
		return newArr
	}							
	console.log(doArr(arr,'size')) //["4", "4", "4", "4", "4", "4", "4", "4", "4"]
	console.log(doArr(arr,'time'))//["2019-02-26 15:10:17", "2019-02-26 15:13:40", "2019-02-28 17:10:06", "2019-02-28 17:12:52", "2019-02-28 17:21:30", "2019-02-28 17:32:00", "2019-02-28 17:38:00", "2019-02-28 17:48:00", "2019-02-28 17:57:01"]

2.数值数组

(1)进行分段查个数

var arr = ['1','50','80','90','4']
	function getCount(arr,type,num1,num2){
		var newArr=[]
		for(let i in arr){
			var a
			if(typeof(arr[i])=='number'){
				a=arr[i]
			}else if(typeof(arr[i])=='string'){
				a=Number(arr[i])
			}
			if(type=='>'){
				if(a>num1){
					newArr.push(a)
				}
			}else if(type=='<'){
				if(a<num1){
					newArr.push(a)
				}
			}else if(type=='><'){
				if(a>=num1&&a<=num2){
					newArr.push(a)
				}
			}
		}
		return newArr.length
	}							
	console.log(getCount(arr,'<',50))   //2
	console.log(getCount(arr,'>',80))   //1
	console.log(getCount(arr,'><',50,80))   //2

二.时间

1.毫秒数指定日期格式

function changeTime(time){
            if(time){
                var oDate = new Date(time*1000),//秒数*1000   毫秒数*1
                    oYear = oDate.getFullYear(),
                    oMonth = oDate.getMonth()+1,
                    oDay = oDate.getDate(),
                    oHour = oDate.getHours(),
                    oMin = oDate.getMinutes(),
                    oSen = oDate.getSeconds(),
                    oTime = oYear +'-'+ getBz(oMonth) +'-'+ getBz(oDay) +' '+ getBz(oHour) +':'+ getBz(oMin) +':'+getBz(oSen);//拼接时间
                return oTime;
            }else{
                return "";
            }
            //补0
            function getBz(num){
                if(parseInt(num) < 10){
                    num = '0'+num;
                }
                return num;
            }

        }
changeTime(1534301216)  //"2018-08-15 10:46:56"
function getLocalTime(nS, f) {
    let format = 'YYYY-MM-DD hh:mm:ss' // 日期格式,yyyy: 年,m:月,d:天,hh:时,mm:分,ss:秒
    if (f) {
      format = f
    }

    let time
    if (nS) {
      time = new Date(nS * 1000)
    } else {
      time = new Date()
    }
    let y = time.getFullYear()
    let m = time.getMonth() + 1
    let d = time.getDate()
    let h = time.getHours()
    let mm = time.getMinutes()
    let s = time.getSeconds()

    m = m < 10 ? '0' + m : m
    d = d < 10 ? '0' + d : d
    h = h < 10 ? '0' + h : h
    mm = mm < 10 ? '0' + mm : mm
    s = s < 10 ? '0' + s : s

    let fullTime = format.replace('YYYY', y).replace('MM', m).replace('DD', d).replace('hh', h).replace('mm', mm).replace('ss', s)

    return fullTime
  }
getLocalTime(1534301216,'YYYY/MM/DD hh:mm:ss') //"2018/08/15 10:46:56"

三.网址

1.当前网址

//1.主机:
window.location.host
//2.路径
window.location.pathname
//3.端口:
window.location.port

2.正常网址

(1)获取参数值

function GetUrlParam(url, name) {
        var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
        var matcher = pattern.exec(url);
        var items = null;
        if (null != matcher) {
            try {
                items = decodeURIComponent(decodeURIComponent(matcher[1]));
            } catch (e) {
                try {
                    items = decodeURIComponent(matcher[1]);
                } catch (e) {
                    items = matcher[1];
                }
            }
        }
        return items;
    }

(2)更改参数值

function replaceParamVal(paramName,replaceWith) {
    var oUrl = this.location.href.toString();
    var re=eval('/('+ paramName+'=)([^&]*)/gi');
    var nUrl = oUrl.replace(re,paramName+'='+replaceWith);
    this.location = nUrl;
window.location.href=nUrl
}

四、网页中效果

1.tab切换

function tabsChange(el,e2) { //e1是点击的一组元素,e2是切换显示的元素
            el.click(function () {
                var _index = $(this).index()
                $(this).siblings().removeClass('on')
                $(this).addClass('on')
                e2.eq(_index).siblings().removeClass('on')
                e2.eq(_index).addClass('on')
            })
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值