数组乱序、多行省略号、mongoose-update

一、数组乱序html

function shuffle(arr){
    var len = arr.length,
        temp;
    for(var i=0,rand;i<len;i++){
        rand = i + ~~(Math.random()*(len - i));
        temp = arr[i];
        arr[i] = arr[rand];
        arr[rand] = temp;
    }
    return arr;
}

console.log(shuffle([5,4,0,1,2,7]));

因为使用sort加Math.random()*0.5实现的排序乱序有很大机会在同一个位置出现的概率比较高,并且每一个位置的概率都不平均。mongodb

而以上的实现能够证明比较优的方法。json

数学概括法:api

证实: i/(i+1)*(i-1/i)...2/3*1/2 = 1/(i+1)数组

一、n=2时,替换概率为1/2,不替换为1/2.安全

二、n=i,i>2时,每一个位置替换与不替换的概率为1/i.dom

三、n=i+1,i+1>2时,每一个位置替换与不替换的概率为1/(i+1),而假设每一个与最后一个替换的几率为1/(i+1),那么最后一个与每一个不替换的几率就是mongoose

i/(i+1),那么循环每个与最后一位替换,第一次的不替换为i/(i+1),第二次就为i,并且根据2假设,每个位置不替换为1/i,那么循环i次为 i/(i+1)*1/i=1/(i+1)atom

 

二、多行省略号spa

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    .mutil-line-ellipsis {
    width: 400px;
    height: 50px;
    line-height: 25px;
    margin: 20px 20px 50px 300px;
    border: 5px solid #AAA;
    line-height: 25px;
    overflow: hidden;
}
/*至关于以前的prop*/
.mutil-line-ellipsis:before {
    content: '';
    float: left;
    width: 5px;/*缩小宽度为5px,其他属性不变*/
    height: 50px;
}
/*至关于以前的main*/
.mutil-line-ellipsis > :first-child {
    float: right;
    width: 100%;
    margin-left: -5px;/*让main元素左移5px,这样main元素在宽度上就彻底占满了父元素;*/
    word-break: break-all;
}
/*至关于以前的realEnd*/
.mutil-line-ellipsis:after {
    content: '...';
    box-sizing: content-box;
    float: right;
    position: relative;
    width: 50px;
    height: 25px;
    top: -25px; /*等于高度的负值,就是文字的行高*/
    left: 100%;
    /*而设置margin-left: -50px、padding-right: 5px则是为了让realend元素的盒模型的最终宽度计算为5px。*/
    margin-left: -50px;
    padding-right: 5px;
    font-size: 13px;
    text-align: right;
    background: linear-gradient(to right, rgba(255, 255, 255, 0), #ffffff 40px);
}
    </style>
</head>
<body>
<div class="mutil-line-ellipsis">
    <div>
        2.main 这里是要多行文本溢出省略的,内容多一点,内容多一点,内容多一点,内容多一点
        内容多一点,内容多一点,内容多一点,内容多一点,内容多一点,内容多一点
    </div>
</div>
</body>
</html>

巧妙使用了relative、margin-left、float,首先使用before挤出5px,而后margin-left:-5px占回去,并且width:100%,那么右边就会出现一点点空间让after占着当内容溢出时,而...为英文默认不换行,那么

就实现了内容溢出出现省略号了。

固然里面还有不少有意思的样式。

 

三、mongoose-update

更新子文档内容,
如如下,因为cartList为数组结构,因此cartList.$.productNum更新到具体某个位置的值,而$为占位符,表明某一行数据下的productNum数据更新
参数:
条件 «Object»
更新内容 «Object»
[操做参数] «Object» 参考https://mongoosejs.com/docs/api.html#query_Query-setOptions
[回调] «Function»
例子:
  User.update({"userId":userId,"cartList.productId":productId},{
    "cartList.$.productNum":productNum,
    "cartList.$.checked":checked,
  }, function (err,doc) {
    if(err){
      res.json({
        status:'1',
        msg:err.message,
        result:''
      });
    }else{
      res.json({
        status:'0',
        msg:'',
        result:'suc'
      });
    }
  })
  删除某一条信息,使用update与原子操做$pull实现,先查询到而后删除符合条件的子文档
  $pull删除符合条件的内容
  User.update({
    userId:userId
  },{
    $pull:{
      'cartList':{
        'productId':productId
      }
    }
  }, function (err,doc) {
    if(err){
      res.json({
        status:'1',
        msg:err.message,
        result:''
      });
    }else{
      res.json({
        status:'0',
        msg:'',
        result:'suc'
      });
    }
  })

MongoDB 原子操做

 

序列化

将对象的状态信息转换为能够存储或传输的形式的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。之后,能够经过从存储区中读取或反序列化对象的状态,从新建立该对象。可序列化的对象在安全上须要注意。

相关文章
相关标签/搜索