当前位置:网站首页>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]) || '--';
};
边栏推荐
- qt creater搭建osgearth环境(osgQT MSVC2017)
- English语法_副词 - ever / once
- C# wpf 通过绑定实现控件动态加载
- 坐标 转化
- Haas506 2.0 development tutorial - Advanced Component Library -modem Net (only supports versions above 2.2)
- Haas506 2.0 development tutorial - Advanced Component Library -modem Voicecall (only supports versions above 2.2)
- Haas506 2.0 development tutorial - Advanced Component Library -modem Sim (only supports versions above 2.2)
- haas506 2.0开发教程-hota(仅支持2.2以上版本)
- Topic35——34. 在排序数组中查找元素的第一个和最后一个位置
- js创建数组(元素都是对象)
猜你喜欢

Haas 506 2.0 Tutoriel de développement - bibliothèque de composants avancés - modem. SMS (ne prend en charge que les versions supérieures à 2,2)

杂七杂八的东东

Get to know webassembly quickly

【接口自动化】软件测试涨薪核心技能、让薪资涨幅200%

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

Day_ 12 smart health project jasperreports

Programmers' real ideas | daily anecdotes

解读创客教育中的团结协作精神

Docker practice - redis cluster deployment and micro service deployment project

js创建数组(元素都是对象)
随机推荐
杂七杂八的东东
C Advanced Learning -- Reflection
【接口自动化】软件测试涨薪核心技能、让薪资涨幅200%
Termux
2.17 haas506 2.0开发教程-system(仅支持2.2以上版本)
C# wpf 通过绑定实现控件动态加载
Day_ 08 smart health project - mobile terminal development - physical examination appointment
golang正则regexp包使用-04-使用正则替换(ReplaceAll(),ReplaceAllLiteral(),ReplaceAllFunc())
bootstrap如何清除浮动的样式
phpStudy设置301重定向
坐标 转化
Docker实战 -- 部署Redis集群与部署微服务项目
Golang regular regexp package use -04- use regular replacement (replaceall(), replaceallliteral(), replaceallfunc())
Day_ 07 smart communication health project FreeMarker
haas506 2.0开发教程-高级组件库-modem.voiceCall(仅支持2.2以上版本)
Leetcode notes: Weekly contest 298
30 data visualization tips that can not be ignored
idea的去除转义的复制粘贴
Summary of business logic security ideas
Qt使用多线程编译项目的方法