0
点赞
收藏
分享

微信扫一扫

前端在开发中使用的技巧和问题记录

weipeng2k 2022-02-03 阅读 20

JS

使用 && 替代 if

&& 返回值 : 
true && 'val' // val
false && 'val' // false

左为true 的时候返回右边的值
左为false的时候返回左边的值

//可以根据这一特性 当 if 使用
isTrue && Fun()

使用 switch 替代 if else

let type=1
if(type==1){
	...
}else if(type==2){
    ...
}
...
//使用switch
switch(type){
	case type==1:
		...
		break;
	case type==2:
		...
		break;
}
...

获取后端返回数据使用 [] or {}

永远不要相信后端返回的数据,当后端返回值为null undefined 时候可以这样

//返回数组
 let res = (await resFun()).data || [] 
 //返回对象
 let res = (await resFun()).data || {}

获取最后一位数字

const num=12312312
const num2='655234234'

console.log(num%10) //2
console.log(num2%10) //4

VUE

工具

lodash

  • 是一套工具库,内部封装了很多字符串、数组、对象等常见数据类型的处理函数(数组对象去重,深拷贝,浅拷贝,防抖,节流,数组分割…)

loadsh中文文档

数组对象去重

var list= [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
list = _.uniqWith(list, _.isEqual)
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
举报

相关推荐

0 条评论