1. 使用 Array.includes 来处理多重条件

// 条件语句
function test(fruit) {
if (fruit == 'apple' || fruit == 'strawberry') {
console.log('red');
}
}
function test(fruit) {
// 把条件提取到数组中
const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; if (redFruits.includes(fruit)) {
console.log('red');
}
}

2. 少写嵌套,尽早返回

function test(fruit, quantity) {
const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; // 条件 1:fruit 必须有值
if (fruit) {
// 条件 2:必须为红色
if (redFruits.includes(fruit)) {
console.log('red'); // 条件 3:必须是大量存在
if (quantity > 10) {
console.log('big quantity');
}
}
} else {
throw new Error('No fruit!');
}
} // 测试结果
test(null); // 报错:No fruits
test('apple'); // 打印:red
test('apple', 20); // 打印:red,big quantity
/_ 当发现无效条件时尽早返回 _/

function test(fruit, quantity) {
const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries']; if (!fruit) throw new Error('No fruit!'); // 条件 1:尽早抛出错误
if (!redFruits.includes(fruit)) return; // 条件 2:当 fruit 不是红色的时候,直接返回 console.log('red'); // 条件 3:必须是大量存在
if (quantity > 10) {
console.log('big quantity');
}
}

3. 相较于 switch,Map / Object 也许是更好的选择

function test(color) {
// 使用 switch case 来找到对应颜色的水果
switch (color) {
case 'red':
return ['apple', 'strawberry'];
case 'yellow':
return ['banana', 'pineapple'];
case 'purple':
return ['grape', 'plum'];
default:
return [];
}
} //测试结果
test(null); // []
test('yellow'); // ['banana', 'pineapple']
// 使用对象字面量来找到对应颜色的水果
const fruitColor = {
red: ['apple', 'strawberry'],
yellow: ['banana', 'pineapple'],
purple: ['grape', 'plum']
}; function test(color) {
return fruitColor[color] || [];
}
或者,你也可以使用 Map 来实现同样的效果: // 使用 Map 来找到对应颜色的水果
const fruitColor = new Map()
.set('red', ['apple', 'strawberry'])
.set('yellow', ['banana', 'pineapple'])
.set('purple', ['grape', 'plum']); function test(color) {
return fruitColor.get(color) || [];
}

map() 方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组。

 const fruits = [
{ name: 'apple', color: 'red' },
{ name: 'strawberry', color: 'red' },
{ name: 'banana', color: 'yellow' },
{ name: 'pineapple', color: 'yellow' },
{ name: 'grape', color: 'purple' },
{ name: 'plum', color: 'purple' }
]; function test(color) {
// 使用 Array filter 来找到对应颜色的水果 return fruits.filter(f => f.color == color);
}

4. 使用 Array.every 和 Array.some 来处理全部/部分满足条件

const fruits = [
{ name: 'apple', color: 'red' },
{ name: 'banana', color: 'yellow' },
{ name: 'grape', color: 'purple' }
]; function test() {
let isAllRed = true; // 条件:所有的水果都必须是红色
for (let f of fruits) {
if (!isAllRed) break;
isAllRed = (f.color == 'red');
} console.log(isAllRed); // false
}
const fruits = [
{ name: 'apple', color: 'red' },
{ name: 'banana', color: 'yellow' },
{ name: 'grape', color: 'purple' }
]; function test() {
// 条件:(简短形式)所有的水果都必须是红色
const isAllRed = fruits.every(f => f.color == 'red'); console.log(isAllRed); // false
}
清晰多了对吧?类似的,如果我们想要检查是否有至少一个水果是红色的,我们可以使用 Array.some 仅用一行代码就实现出来。
const fruits = [
{ name: 'apple', color: 'red' },
{ name: 'banana', color: 'yellow' },
{ name: 'grape', color: 'purple' }
]; function test() {
// 条件:至少一个水果是红色的
const isAnyRed = fruits.some(f => f.color == 'red'); console.log(isAnyRed); // true
}

本文参考掘金大佬Hopsken!

map() 方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组。

最新文章

  1. STM32 HAL固件库编程的文件构架
  2. Newtonsoft.Json 把对象转换成json字符串
  3. SSH原理与运用(一):远程登录
  4. sql整型字段模糊查询
  5. js中的运动
  6. POJ 1753 Flip Game 状态压缩,暴力 难度:1
  7. CKEditor在线编辑器增加一个自定义插件
  8. 如何在网页中显示pdf
  9. Flex和Servlet结合上传文件报错(二)
  10. iOS离线打包
  11. 【JAVAEE学习笔记】hibernate03:多表操作详解、级联、关系维护和练习:添加联系人
  12. 简述ADO.NET的连接层
  13. 转载-《Python学习手册》读书笔记
  14. failed to create pid file /var/run/rsyncd.pid: File exists报错
  15. [转帖] bat方式遍历目录内的文件
  16. this.$route和this.$router的区别
  17. vue全家桶+Koa2开发笔记(4)--redis
  18. 【BZOJ1201】[HNOI2005]数三角形(暴力)
  19. oracle分区交换技术
  20. apply新用法,最大值查找

热门文章

  1. 每日命令:(8)cp
  2. virtualenv与virtualenvwrapper
  3. SpringSecurity 获取认证信息 和 认证实现
  4. 浅谈href=与href=javascript_void(0)的区别
  5. noip模拟赛 三角形
  6. 洛谷—— P2658 汽车拉力比赛
  7. Codeforces Goodbye2016
  8. ScrollView双击图片定点放大
  9. Hive之内置函数
  10. 错误代码: 1045 Access denied for user 'skyusers'@'%' (using password: YES)