javascript array examples


/ Published in: JavaScript
Save to your folder(s)

javascript arrays up and down


Copy this code and paste it in your HTML
  1. //--------removing item from array
  2. existingArray.splice( znum, 1); //znum is the spot to remove 0-(length-1)
  3. //1 could be more if you want to remove more
  4.  
  5. //--------sort array by keys
  6. //xOrderList[0] = { "name" : "Fred" , "ranktype" : "45" };
  7. //xOrderList[1] = { "name" : "Jed" , "ranktype" : "35" };
  8. //xOrderList[2] = { "name" : "Red" , "ranktype" : "55" };
  9. xOrderList.sort(function(a,b) { return parseFloat(b.ranktype) - parseFloat(a.ranktype) } ); //highest to lowest
  10. xOrderList.sort(function(a,b) { return parseFloat(a.ranktype) - parseFloat(b.ranktype) } ); //lowest to hights by ranktype
  11.  
  12. //--------duplicate an array
  13. var xNewArray = existingArray.slice();
  14. //note that if the array contains arrays or objects, this will not work as they are just pointers to those items
  15.  
  16. //--------duplicate an array of arrays
  17. function duplicateArray( xArray ) {
  18. var xTempArray = [];
  19. console.debug( xArray.length );
  20. for (var x=0; x<xArray.length; x++){
  21. xTempArray.push( xArray[x].slice() );
  22. }
  23. return xTempArray;
  24. }
  25. var xNewArray = duplicateArray( existingArray );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.