includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。
比如:
if (list.incidentType === 1 || list.incidentType === 2 || list.incidentType === 3 || list.incidentType === 4 || list.incidentType === 5 || list.incidentType === 6 || list.incidentType === 7 || list.incidentType === 8 || type
=== 17 || list.incidentType === 18 || list.incidentType === 21 || list.incidentType === 23 || list.incidentType === 24 || list.incidentType === 25 || list.incidentType === 26 || list.incidentType === 27 || list.incidentType === 28 || list.incidentType === 29
) {
list.icon = '/assets/glodCenter/icon/jl.png'
}else if (list.incidentType === 11 || list.incidentType === -1 || list.incidentType === 16 || list.incidentType === 19 || list.incidentType === 20 || list.incidentType === 30
|| list.incidentType === 31
) {
list.icon = '/assets/glodCenter/icon/xh.png'
}
上面的可以简写成
//奖励相关
const listArr1 = [1,2,3,4,5,6,7,8,17,18,21,23,24,25,26,27,28,29]
// 消耗相关
const listArr2 = [-1,11,16,19,20,30,31]
// 好友相关
const listArr3 = [12,13,14,15]
if (listArr1.includes(list.incidentType)) {
list.icon = '/assets/glodCenter/icon/jl.png'
} else if (listArr2.includes(list.incidentType)) {
list.icon = '/assets/glodCenter/icon/xh.png'
} else if (listArr3.includes(list.incidentType)) {
list.icon = '/assets/glodCenter/icon/hy.png'
}
网友评论