精度问题

1、原生解决 toFixed
parseFloat((a+b).toFixed(2))

2、math库

  • math库使用
    1. //统一配置math.js
    2. math.config({
    3. number: 'BigNumber',
    4. // 'number' (default),
    5. precision: 20
    6. });
    7. // 转换数字类型
    8. var temp = math.bignumber(a) * math.bignumber(b)
    9. // 提取数字类型,不然会是一个math对象
    10. var result = math.number(temp)
    3、自定义函数
    function accMul(arg1,arg2){
    var m=0,s1=arg1.toString(),s2=arg2.toString();
    try{m+=s1.split(“.”)[1].length}catch(e){}
    try{m+=s2.split(“.”)[1].length}catch(e){}
    return (Number(s1.replace(“.”,””))*Number(s2.replace(“.”,””))/Math.pow(10,m)).toFixed(2);
    }

    无限滚动

    ```javascript app.addEventListener(‘scroll’,()=>{ //变量scrollTop是滚动条滚动时,距离顶部的距离 var scrollTop = app.scrollTop; //变量windowHeight是可视区的高度 var windowHeight = app.clientHeight; //变量scrollHeight是滚动条的总高度 var scrollHeight = app.scrollHeight; //滚动条到底部的条件 if (scrollTop+windowHeight+300>=scrollHeight) { if(!lock){
    1. return
    } lock = false this.pushDetail() setTimeout(() => {
    1. lock = true
    },200) } })

pushDetail(){ if (this.pageSize * this.pageNo >= this.total) { return } const that = this; this.pageNo = this.pageNo + 1; $.ajax({ type:’get’, url:”http://39.107.232.95:7065/jkb_job/group/user/list/info?jkbGroupId=“ + this.jkbGroupId + “&pageNo=” + this.pageNo + “&pageSize=” + that.pageSize, success(res){ res.result.data.filter(item=>{ that.showGroup.push(item) console.log(this.showGroup); }) } }) },

  1. <a name="bKjT2"></a>
  2. ## 对象去重
  3. ```javascript
  4. const arr = [
  5. { name: "name1", num: "1" },
  6. { name: "name1", num: "1" },
  7. { name: "name2", num: "11" },
  8. { name: "name3", num: "12" },
  9. { name: "name4", num: "13" },
  10. { name: "name2", num: "1" },
  11. { name: "name6", num: "12" }
  12. ]
  13. let obj = {};
  14. var a = arr.reduce((cur,next) => {
  15. obj[next.name]?'':obj[next.name]=true && cur.push(next)
  16. return cur;
  17. },[])

判断是否为图片

  1. /\.(gif|jpg|jpeg|png|GIF|JPEG|JPG|PNG)$/.test('xxx.png')

生成随机数

  1. getRandomCode(len) { //len为随机数的位数
  2. let code = '';
  3. for (let i = 0; i < len; i++) {
  4. let r = Math.random();
  5. code += Math.floor(r * Math.pow(10, 1)).toString();
  6. }
  7. code = code.replace('4', '8');
  8. return code;
  9. }

indexDB的使用

https://www.npmjs.com/package/indexdb-storage
1、安装

  1. npm i indexdb-storage --save

2、引入

  1. import { IndexDBStorage } from 'indexdb-storage'

3、使用

  1. const dbStorage = new IndexDBStorage({
  2. name: 'testdb'
  3. })
  4. // 数据设置
  5. dbStorage.setItem('hello', {
  6. a: 1,
  7. b: {
  8. c: 2
  9. }
  10. })
  11. // 数据获取
  12. dbStorage.getItem('hello').then((d) => {
  13. console.log(d)
  14. })
  15. // 数据清除
  16. dbStorage.clean()