If 的简便写法
let newPath = "";
if (item.id == 0) {
newPath = "/examination/sectionTopics";
} else if (item.id == 1) {
newPath = "/examination/knowledgeTopics";
} else if (item.id == 2) {
newPath = "/examination/testTopics";
} else if (item.id == 3) {
newPath = "/examination/papers";
} else if (item.id == 4) {
newPath = "/examination/management";
}
可以这样写
let newPath = {
0: "/examination/sectionTopics",
1: "/examination/knowledgeTopics",
2: "/examination/testTopics",
3: "/examination/papers",
4: "/examination/management"
}[item.id];
如果进了else 还有操作 , 就可以这样写
let newPath = {
0: "/examination/sectionTopics",
1: "/examination/knowledgeTopics",
2: "/examination/testTopics",
3: "/examination/papers",
4: "/examination/management"
}[item.id] || -1
另外一种场景
if(a == 1 ){
a += 1
}
或者
if(a == 1) a += 1
新的数组方法:array.at()
let arr = [1,2,3,4,5,6,7,8,9]
获取最后一个元素
arr[ arr.length - 1 ]
// 9
可以更换为
arr.at(-1)
// 9
at() 方法接收一个整数值并返回该索引的项目,允许正数和负数。负整数从数组中的最后一个项目开始倒数。
方括号符号没有问题,但对于后面的项目,可以调用array.at(-1),无须再访问 array.length
数组方法:includes用法
// 原写法
if (x === "abc" || x === "def" || x === "ghi" || x === "jkl") {
}
// 简写
if (["abc", "def", "ghi", "jkl"].includes(x)) {
}
//解析
//1.数组用法
//includes 可以判断一个数组中是否包含某一个元素,并返回true 或者false
['a','b','c'].includes('a')
true
['a','b','c'].includes(1)
false
//includes可以包含两个参数,第二个参数表示判断的起始位置,起始位置第一个数字是0。
['a','b','c'].includes('a',2) // 第二个位置是否是'a'
false
//2.字符串用法 查找字符串是否包含 "Runoob":
var str = "Hello world, welcome to the Runoob。";
var n = str.includes("Runoob");
n 输出结果:
true