Put elements to array with condition - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

Put elements to array with condition

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  w  ww  . ja  v a  2 s. c o  m*/
var arr = [
  {id: 5, attr: 99},
  {id: 7, attr: null},
  {id: 2, attr: 81},
  {id: 9, attr: 31111},
  {id: 4, attr: null}
]

var withNull = arr.filter(function (el) { return el.attr === null });
var withVal = arr.filter(function (el) { return el.attr !== null });

withVal.unshift.apply(withVal, withNull);

console.log(withVal)
    }

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

Related Tutorials