当前位置:网站首页>js中if逻辑过多,常见优化
js中if逻辑过多,常见优化
2022-06-23 05:37:00 【天渺工作室】
1. 单一判断常见优化
if判断逻辑过多,在常见code review 中,下面的这种情况肯定是要被批评的。
const gitObj = (type) => {
if (type === '张三') {
return '张三的Obj';
}
if (type === '李四') {
return '李四的Obj';
}
if (type === 233) {
return 'testObj';
}
if (type === 'testUser') {
return 'testUser';
}
return '--';
};
单一判断常见优化方式
const gitObj = (type) => {
const newMap = {
'张三': '张三的Obj',
'李四': '李四的Obj',
233: 'testObj',
'testUser': 'testUser哈哈哈',
};
return newMap[type] || '--';
};
2. 多个判断逻辑的常见优化
多个判断逻辑过多
const gitObj = (type, name) => {
if (type === '张三') {
return '张三的Obj';
}
if (type === '李四' && name === '李四type') {
return '李四Obj';
}
if (type === '李四' && name === '李四typePlus') {
return '李四ObjPlusObj';
}
if (type === '小明' && name === '小明type') {
return '小明Obj';
}
if (type === '小明' && name === '小明typePlus') {
return '小明ObjPlus';
}
return '--';
};
优化方式1
const gitObj = (type, name) => {
let newArr = [
['张三', false, '张三的Obj'],
['李四', '李四type', '李四Obj'],
['李四', '李四typePlus', '李四ObjPlusObj'],
['小明', '小明type', '小明Obj'],
['小明', '小明typePlus', '小明ObjPlus']
];
let res = newArr.filter((item) => {
return type === newArr[0] && (type.name === newArr[1] || !!type.name === !!newArr[1]);
});
return res.length === 0 ? '--': res[0][2];
};
优化方式2
const gitObj = (type, name) => {
const map = {
'张三': '张三的Obj',
'李四': {
'李四type': '李四Obj',
'李四typePlus': '李四ObjPlusObj',
},
'小明': {
'小明type': '小明Obj',
'小明typePlus':"小明ObjPlus"
}
}
return (name ? map[type][name] : map[type]) || '--';
};
边栏推荐
- Haas506 2.0 development tutorial - Advanced Component Library -modem SMS (only supports versions above 2.2)
- 什么是客户体验自动化?
- Home address exchange
- Functions and basic structure of CPU
- Index - MySQL
- 微信小程序 - 全局监听globalData的某个属性变化,例如监听网络状态切换
- Day_ 11 smart communication health project - graphic report and poi Report
- haas506 2.0开发教程-hota(仅支持2.2以上版本)
- Illuminate\support\collection de duplication unique list de duplication
- Docker practice - redis cluster deployment and micro service deployment project
猜你喜欢

How to view native IP

常见设置模式(抽象工厂&责任链模式&观察者模式)

Illustration Google V8 18: asynchronous programming (I): how does V8 implement micro tasks?

项目_过滤器Filter解决中文乱码

c#数据库报错问题大家帮我看看吧

Vs+qt project transferred to QT Creator

English语法_形容词比较级 - 3级变化

Summary of business logic security ideas

Synchronous switching power supply reduces EMI layout dv/dt di/dt

mysql如何将日期转为数字
随机推荐
Illustration Google V8 18: asynchronous programming (I): how does V8 implement micro tasks?
C language stepping on the pit: document coding error, resulting in Base64 Chinese coding error
Verilog语法讲解
【系统】右键桌面图标,转圈后,资源管理器就崩溃,桌面就重新刷新
How to build a data application system based on overall value for energy enterprises
20220621 Dual Quaternion
如何迁移virtualbox 的虚拟机到hype-v
haas506 2.0開發教程-高級組件庫-modem.sms(僅支持2.2以上版本)
MySQL ON DUPLICATE KEY 和 PgSQL ON CONFLICT(主键) 处理主键冲突
ffplay实现自定义输入流播放
C语言学习总结
The softing datafeed OPC suite stores Siemens PLC data in an Oracle Database
mysql如何将日期转为数字
页面嵌入iframe 点击浏览器后退问题
Illuminate\support\collection de duplication unique list de duplication
asp.net文件下载demo与相关问题的处理
解析创客教育中的造物原理
中台库存中的实仓与虚仓的业务逻辑设计
了解学习 JSX 的工作方式
Open source to the world (Part 2): the power of open source from the evolution of database technology BDTC 2021