Loop through sparse array and replace sparse values with map function - Javascript Array

Javascript examples for Array:map

Description

Loop through sparse array and replace sparse values with map function

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(){// ww  w.j  a  v a  2s .  com
var myarray = ['foo',, 'bar', , , , ];
function Fill(n, _default) {
  return Array.apply(null,n).map(function(val) {
    console.log("val"+val);
    return val || _default;
  });
}
var newa = Fill(myarray, "default");
console.log(JSON.stringify(newa));
    }

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

Related Tutorials