Thread: Help Array!!!!!
View Single Post
Old 12-31-2012, 04:30 AM   PM User | #15
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by triko View Post
But if I want put in array2 only the even or odd number???
How to do???
The .slice method generates a new array from an existing one.
For recent browsers that support the .filter method, you can just do:

Code:
<script type="text/javascript">

array1 = [ 1, 4, 2, 13, 5 ];

odds = array1.filter( function( elem ){ return elem % 2; } )

alert( odds );

</script>

Last edited by Logic Ali; 12-31-2012 at 04:45 AM..
Logic Ali is offline   Reply With Quote
Users who have thanked Logic Ali for this post:
jmrker (12-31-2012)