1

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.

1 Answers1

2

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)
Ori Drori
  • 183,571
  • 29
  • 224
  • 209