Javascript Data Type How to - Convert String to array then remove last element








Question

We would like to know how to convert String to array then remove last element.

Answer


<!DOCTYPE html>
<html>
<body>
<script type='text/javascript'>
<!-- w ww  .j  a  v a  2  s . c om-->
var path = "/bar/foo/moo/";
var split = path.split("/");
var x = split.slice(0, split.length - 2).join("/") + "/";
document.writeln(x);

</script>
</body>
</html>

The code above is rendered as follows: