Skip to content
CodeWorth
CodeWorth

Code worths when shared

Primary Navigation Menu
Menu
  • BLOG
    • DataBase
    • Programming
    • Mobile
    • Multimedia
    • OS Tips
    • Others
    • Web Development
  • Products
    • Utilities
    • Games
    • JsGenLib
      • PlugIns
      • Core Functions
      • Helper & ShortHands
  • About

Javascript Arrays – Quick reference

On September 9, 2009
In Web Development
Tagged JavaScript, Web
With 0 Comments
Declaring arrays
var myarray=["Bob", "Bully", "Amy"];
Sorting arrays
  • It’s done by array.sort()
  • It’s a method that’s probably as much misunderstood as it is underestimated
  • Sort’s in ascending order. no direct way to sort in descending order
var myarray=["Bob", "Bully", "Amy"];
myarray.sort(); // Array now becomes ["Amy", "Bob", "Bully"]
  • Sort’s in lexicographical (aka alphabetical) order. Numeric sort not allowed
var myarray=[7, 40, 300];
myarray.sort();//Array now becomes [300,40,7]
//Numerically 7<40<300 but lexicographically 7>40>300.
//So we shouldn't use it for numeric sort. (array.reverse() may be used)
Customized Sorting of arrays
  • Customized sort with a function argument to array.sort()
  • The function should have 2 arguments
  • The function should return integer value based on following conditions
    • Less than 0(negative): Sort “a” to be a lower index than “b”.
    • Zero: “a” and “b” should be considered equal, and no sorting performed.
    • Greater than 0(positive): Sort “b” to be a lower index than “a”.
function sortfunction(a, b)
{
	//Compare "a" and "b" in some fashion, and return -1, 0, or 1
}
var myarray=["Bob", "Bully", "Amy"];
myarray.sort(sortfunction); //array will be sorted with fashion used.
  • Sorting an array in numerical ascending
var myarray=[25, 8, 7, 41];
myarray.sort(function(a,b)
{
	return a - b;
}); //Array is [7, 8, 25, 41]
  • Sorting an array in numerical descending
var myarray=[25, 8, 7, 41];
myarray.sort(function(a,b){
	return b - a;
}); //Array is [41, 25, 8, 7]
  • Shuffle array by Sorting an array randomly
var myarray=[25, 8, 7, 41];
myarray.sort(function()
{
	return 0.5 - Math.random();
}); //Array is now scrambled

Reversing array
  1. It’s done by array.reverse()
  2. Will change the original array itself
  3. if n is no.of.objects in array. Then object at xth position will be swapped with (n-1)th object.
var myarray=["Bob", "Bully", "Amy"];
myarray.reverse(); //Array now becomes ["Bully", "Bob", "Amy"]
Sort array alphabetically in descending order
  1. Can be done with usage of sort and then reverse.
var myarray=["Bob", "Bully", "Amy"]
myarray.sort();
myarray.reverse(); //Array now becomes ["Bully", "Bob", "Amy"]
Stack
  1. A stack follows the Last-In First-Out (LIFO) paradigm
  2. An item added last will be removed first.
  3. The Array class has 2 methods that provide stack functionality. they are push() and pop().
    • push() appends an item to the end of the array
    • pop() removes and returns the last item in the array
var stack = [];
stack.push(2);  // stack is now [2]
stack.push(5);  // stack is now [2, 5]
var i = stack.pop(); // stack is now [2]
alert(i);   // displays 5
Queue
  1. A queue follows the First-In First-Out (FIFO) paradigm.
  2. The first item added will be the first item removed.
  3. The Array class has 2 methods that provide queue functionality. they are push() and shift().
    • push() inserts the passed argument at the end of the array.
    • shift() removes and returns the first item.
var queue = [];
queue.push(2);  // queue is now [2]
queue.push(5);  // queue is now [2, 5]
var i = queue.shift();  // queue is now [5];
alert(i);   // displays 2
For Queue and Stack these function names may be confusing so we can always create aliases with our own names as below.
var queue = [];
queue.add = queue.push;
queue.remove = queue.shift;
queue.add(1);
var i = queue.remove();
alert(i);
2009-09-09

Subscribe

Enter your email address to receive notifications about new posts via email.

Join 634 other subscribers

Google

Designed using Chromatic WordPress Theme. Powered by WordPress.