js工作中实际开发使用代码

1、多重嵌套使用 forEach 循环
1-1、

xxx.forEach( (item) => {
    
    
	  item.xxx.forEach( (item) =>{
    
    
				console.log(xxx)
		})
	})

2、正则只取出数字
2-1、详情: 字符串内,包含 数字,文字,取出数字

 const tmp =  '1267708679575048194这里是我们要用正则取出来的数据'

   console.log(tmp.replace(/[^0-9]/ig,""));

3、数组取出对应的下标
3-1、详情:数组内取出 对应的下标

const fruits = [
		{
    
    name:'apples', quantity:2},
		{
    
    name:'banans',quantity:0},
		{
    
    name:'cherries',quantity:5}
	]	

const index = fruits.findIndex( fruit => fruit.name === 'banans' )
console.log(index)
console.log(fruits[index] )
//这里的index 就是 我们所需要的索引

4、js 点击图片 下载 base64 位图片
axios 请求接口

static asycn qrCode ( id ) {
    
    
	const formData = new FormData()
	formData.append('xxx参数',id)
	return axios.post('地址xxxx',formData, {
    
    
		responseType:'arraybuffer',
		heders:{
    
    
			'Content-type': 'application/x-www-form-urlencoded'
		}
	})
}

methods 方法

qrCodeDownload ( id ) {
    
    
	xxx.qrCode(id).then( ( response ) => {
    
    
		 return (
              'data:image/png;base64,' +
              btoa(new Uint8Array(response).reduce((data, byte) => data + String.fromCharCode(byte), ''))
            )
	}) .then( (data) => {
    
    
		  var a = document.createElement('a')
            a.download = 'png' || 'pic'
            a.href = data
            a.click()
	})
}

5、变量的结构赋值
5-1、可以用结构进行遍历交换
let x=1;
let y=2;
[x,y] =[y, x]
这里 x 是 2
y 是 1

6、Vue 定时器清除

data () {
		return :{
			 timer: null, 
		}
} 

我是在 watch 中使用监听,说以我放在 watch 中

watch :{
	 where:{
			  immediate: true,
      		  deep: true,
		      async handler(where) {  // 监听的参数
		        let flag = this.isEmpty(where)  //判断传入的 对象是否为空详情见第七个代码。
		        if (flag === false) {
		          this.setTime()
		        } else {
		          this.setClearTime()
		        }
		        // 上面if 判断定时器,是否清除。
		        await this.$nextTick()
		        this.$refs['table'].refresh(true)
		      },
	}

}

methods 方法中进行声明

    setTime() {
      this.timer = setInterval(() => {
        this.query()
      }, 3000)
    },
	
	 setClearTime() {
      window.clearInterval(this.timer)
    },
	


7、判断一个对象是否为空对象

isEmpty(obj) {
  	return Object.keys(obj).length === 0
	翻译中文就是:返回一个 对象的 key 值的长度是 0 的时候 就是 ture  否则 就是  false  
  },

猜你喜欢

转载自blog.csdn.net/weixin_45796807/article/details/112968750
今日推荐