generate an array with values of another array in javascript - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

generate an array with values of another array in javascript

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from   ww  w .j  ava2  s.  c om*/
arr = [1,2,3,4,5,6,7,8];
Array.prototype.random = function () {
    var result = [],
        that = this.slice();
    this.forEach(function () {
        result.push(that.splice(Math.floor(Math.random() * that.length), 1)[0]);
    });
    return result;
}
document.body.innerHTML = arr.random();
    }

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

Related Tutorials