0

My problem:

I have a path like this fore example: some/path/with/unknown/depth

In this example i want to access depth.

I know i can do it like this:

let path = "some/path/with/unknown/depth";

let word = path.split("/")[4];

But 4 is hardcoded. Is there any way to get the last element dynamically? One solution could be this here:

let path = "some/path/with/unknown/depth";

let words = path.split("/");

let word = words[words.length - 1];

Is it possible to get the last element just with 1 line?

bill.gates
  • 14,145
  • 3
  • 19
  • 47

1 Answers1

1

Hello here's my trick for doing this :)

let path = "some/path/with/unknown/depth";
let result = path.split('/').reverse()[0]
console.log(result);