Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

javascript - How to give `this` the value of caller Object that is an array

I know this is a pretty short question, but I was just wondering if we can short this:

cn.link.split('/')[cn.link.split('/').length - 1]

To this:

cn.link.split('/')[this.length - 1] or something like this where this can be replaced by something to make this work.

NOTE:- cn is an object.

question from:https://stackoverflow.com/questions/65847358/how-to-give-this-the-value-of-caller-object-that-is-an-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use Array.pop() To get the last item of an array:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...