Remove trailing elements from array that are equal to zero - Javascript Array

Javascript examples for Array:slice

Description

Remove trailing elements from array that are equal to zero

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
var arr = [1,2,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var copy = arr.slice(0);/*from   w  ww  . j a v a 2  s. c o  m*/
var number = Number(copy.reverse().join('')).toString().length;
arr.length = number;
console.log(arr);

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

Related Tutorials