AS3 Using Push, Pop, Unshift and Shift on Arrays


/ Published in: ActionScript 3
Save to your folder(s)

I always forget which method does what. This is just as a quick reminder.


Copy this code and paste it in your HTML
  1. var myArray:Array = new Array();
  2.  
  3.  
  4. // PUSH
  5. // Adds one or more elements to the end of an array and returns the new length of the array.
  6. myArray = ["a","b","c","d"];
  7. trace(myArray);
  8. myArray.push("e");
  9. trace(myArray);
  10. // OUTPUT
  11. // a,b,c,d
  12. // a,b,c,d,e
  13.  
  14.  
  15. // POP
  16. // Removes the last element from an array and returns the value of that element.
  17. myArray = ["a","b","c","d"];
  18. trace(myArray);
  19. myArray.pop();
  20. trace(myArray);
  21. // OUTPUT
  22. // a,b,c,d
  23. // a,b,c
  24.  
  25.  
  26. // UNSHIFT
  27. // Adds one or more elements to the beginning of an array and returns the new
  28. // length of the array. The other elements in the array are moved from their
  29. // original position, i, to i+1.
  30. myArray = ["a","b","c","d"];
  31. trace(myArray);
  32. myArray.unshift("_");
  33. trace(myArray);
  34. // OUTPUT
  35. // a,b,c,d
  36. // _,a,b,c,d
  37.  
  38.  
  39.  
  40. // SHIFT
  41. // Removes the first element from an array and returns that element.
  42. // The remaining array elements are moved from their original position, i, to i-1.
  43. myArray = ["a","b","c","d"];
  44. trace(myArray);
  45. myArray.shift();
  46. trace(myArray);
  47. // OUTPUT
  48. // a,b,c,d
  49. // b,c,d

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.