String for the array, then remove the last element

I have the lines below and I am trying to remove the last directory from them, but I cannot figure it out.

Javascript

var x = path.split("/") alert(path +' = ' +x.slice(0, -1)); 

Expected Result

 /foo/bar/ = /foo/ /bar/foo/ = /bar/ /bar/foo/moo/ = /bar/foo/ 
+6
source share
1 answer

Try:

 var path = "/bar/foo/moo/"; var split = path.split("/"); var x = split.slice(0, split.length - 2).join("/") + "/"; alert(x); 

Demo

+8
source

Source: https://habr.com/ru/post/895477/


All Articles