精度问题
1、原生解决 toFixed
parseFloat((a+b).toFixed(2))
2、math库
- math库使用
3、自定义函数//统一配置math.js
math.config({
number: 'BigNumber',
// 'number' (default),
precision: 20
});
// 转换数字类型
var temp = math.bignumber(a) * math.bignumber(b)
// 提取数字类型,不然会是一个math对象
var result = math.number(temp)
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){
} lock = false this.pushDetail() setTimeout(() => {return
},200) } })lock = true
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); }) } }) },
<a name="bKjT2"></a>
## 对象去重
```javascript
const arr = [
{ name: "name1", num: "1" },
{ name: "name1", num: "1" },
{ name: "name2", num: "11" },
{ name: "name3", num: "12" },
{ name: "name4", num: "13" },
{ name: "name2", num: "1" },
{ name: "name6", num: "12" }
]
let obj = {};
var a = arr.reduce((cur,next) => {
obj[next.name]?'':obj[next.name]=true && cur.push(next)
return cur;
},[])
判断是否为图片
/\.(gif|jpg|jpeg|png|GIF|JPEG|JPG|PNG)$/.test('xxx.png')
生成随机数
getRandomCode(len) { //len为随机数的位数
let code = '';
for (let i = 0; i < len; i++) {
let r = Math.random();
code += Math.floor(r * Math.pow(10, 1)).toString();
}
code = code.replace('4', '8');
return code;
}
indexDB的使用
https://www.npmjs.com/package/indexdb-storage
1、安装
npm i indexdb-storage --save
2、引入
import { IndexDBStorage } from 'indexdb-storage'
3、使用
const dbStorage = new IndexDBStorage({
name: 'testdb'
})
// 数据设置
dbStorage.setItem('hello', {
a: 1,
b: {
c: 2
}
})
// 数据获取
dbStorage.getItem('hello').then((d) => {
console.log(d)
})
// 数据清除
dbStorage.clean()