JS 中的小算法

一、用正则验证的IP地址是否合法

checkIpAddr: function(ipaddr)

{

  // console.log("ppppip="+ipaddr);

var  exp=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;

var reg = ipaddr.match(exp); 

if(reg==null) 

    return false;//不合法

else 

    return true;//合法

},

   二、 十六进制字符串转int

  function hex2int(hex) {

    var len = hex.length,

      a = new Array(len),

      code;

    for (var i = 0; i < len; i++) {

      code = hex.charCodeAt(i);

      if (48 <= code && code < 58) {

        code -= 48;

      } else {

        code = (code & 0xdf) - 65 + 10;

      }

      a[i] = code;

    }

    return a.reduce(function (acc, c) {

      acc = 16 * acc + c;

      return acc;

    }, 0);

  }

三、根据对象数组的某个属性值找到指定的元素

对象数组:

var datas = [
    {
      "name": "商品房",
      "bianma": "11"
    },
    {
      "name": "商铺",
      "bianma": "12"
    }
 ]

查找:filter() 方法将匹配元素集合缩减为匹配指定选择器的元素.该方法不改变原数组,返回的是筛选后满足条件的数组.

var data= datas.filter(function(item){
     return item.bianma == "12"; 
})
console.log(data); // [{name: "商铺", bianma: "12"}]

删除该条数据:

datas.splice(datas.indexOf(data), 1) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值