我知道这是一个很短的问题,但我只是想知道我们是否可以缩短这个:
cn.link.split('/')[cn.link.split('/').length - 1]
:
cn.link.split('/')[this.length - 1]
或者类似这样的东西,this
可以被其他东西代替,使这个工作
注意:—cn
是一个对象。
使用Array.pop()
获取数组的最后一项:
const link = 'a/b/c/d/e'
const last = link.split('/').pop()
console.log(last)
const url = `assets/images/products/${link.split('/').pop()}/pro_1.jpg`
console.log(url)