js字符串、数组处理方法、以及一些常用js方法

1.截取获得某字符串后面的字符:

var i = id.substring(id.indexOf("+") + 1, id.length);//获取+后面的字符

2.截取量字符串之间的字符

  • var str = "aaabbbcccdddeeefff";  
  • str = str.match(/aaa(S*)fff/)[1];  
  • alert(str);//结果bbbcccdddeee 

3.返回一个新的数组sindex到eindex

//返回一个新的arr(arr的第sindex到eindex个元素)
function getarry(sindex, eindex, arr) {
var newarr = new Array();
for (var i = sindex; i <= eindex; i++) {
newarr.push(arr[i]);
}
return newarr;
}

4.判断周末

   //周末
    function isworker(dts) {
        var dt = new Date(dts);
        if (dt.getDay() % 6 == 0)
            //周末
            return true;
        else
            //工作日
            return false;
    }

5.检查手机合法性

  function checkMobilePhone(str) {
        if (str.match(/^(?:13d|15d|17d|18d)-?d{5}(d{3}|*{3})$/) == null) {
            return false;
        }
        else {
            return true;
        }
    }

 6.js 获取cookie

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) {
            return decodeURIComponent(c.substring(nameEQ.length, c.length))
        }
    } return null
}

 6.js  json中的时间转换格式

//根据json中的日期格式,转换成yyyy-mm-dd HH:mm:ss
function ChangeDateFormat(cellval) {
var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return date.getFullYear() + "-" + month + "-" + currentDate + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
}

7.获取当前get请求参数

//调用示例 var id = GetRequest().pageId;
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

8.获取当前地点的经纬度

 //获取当前经纬度
        navigator.geolocation.getCurrentPosition( // 该函数有如下三个参数
       function (pos) { // 如果成果则执行该回调函数
           la = pos.coords.longitude;
           lg = pos.coords.latitude;
           //alert(
           //    '  经度:' + pos.coords.latitude +
           //    '  纬度:' + pos.coords.longitude +
           //    '  高度:' + pos.coords.altitude +
           //    '  精确度(经纬):' + pos.coords.accuracy +
           //    '  精确度(高度):' + pos.coords.altitudeAccuracy +
           //    '  速度:' + pos.coords.speed
           //);
       }, function (err) { // 如果失败则执行该回调函数
           alert(err.message);
       }, { // 附带参数
           enableHighAccuracy: true, // 提高精度(耗费资源)
           timeout: 5000, // 超过timeout则调用失败的回调函数
           maximumAge: 1000 // 获取到的地理信息的有效期,超过有效期则重新获取一次位置信息
       });

 9.给json 进行排序

 function sortdistance(a, b) {
            return a.distance - b.distance
        }

  var pointA = new BMap.Point(r.point.lng, r.point.lat);//获取当前地点经纬度
                        for (var i = 0; i < returndata.datas.length; i++) {
                            var pointB = new BMap.Point(returndata.datas[i].lat, returndata.datas[i].lng)
                            returndata.datas[i].distance = map.getDistance(pointA, pointB).toFixed(2);
                        }

     returndata.datas.sort(sortdistance);
原文地址:https://www.cnblogs.com/chongyao/p/6721527.html