Sunday 1 August 2010

Java Script: Array Reference

Array
Arrays are a way of storing multiple values together.
Arrays are best use to store similar values.
Creating Array
var myArray = [];
var myArray = new Array(Number of Element);
var myArray = new Array(Element 1, Element 2,...);
Looping Array
    Easiest way to access all elements.
    var myArray = [1,2,3]; 
    for(var index in myArray){
         document.write(myArray[index]);
    }
    Result
    123
    Traditional programming approach. Can only access contiguous array.
    var myArray = [1,2,3]; 
    for(var index=0; index < myArray.length; index++){
         document.write(myArray[index]);
    }
    Result
    123
    Looping through the array and consuming it contents. Starting with the last element
    var myArray = [1,2,3]; 
    while(myArray.length){
         document.write(myArray.pop());
    }
    Result
    321
    Looping through the array and consuming it contents. Starting from the first element
    var myArray = [1,2,3]; 
    while(myArray.length){
         document.write(myArray.shift());
    }
    Result
    123
    Other Cool methods
    array.concat(array[,array]*) 
    Join two or more array and return the joined array.
    Example
    var myArrayA = [1,3,5];
    var myArrayB = ['a','b','c'];
    var combineArray = myArrayA.concat(myArrayB);
    Result
    myArrayA = [1,3,5]
    myArrayB = ['a','b','c'];
    combineArray = [1,3,5,'a','b','c'];

    array.join()
    concatenate all element of an array into a string.
    Example
    var myArray = ['a','b','c'];
    var myString = myArray.join();
    Result
    myArray = ['a','b','c'];
    myString = "abc";
    array.pop()
    remove the last element of the array.
    array.push()
    insert an element to the end of the array;
    array.shift()
    remove the first element of the array.
    array.unshift()
    insert an element at the beginning of the array.
    Example
    var myArray = ['a','b','c','d','e'];
    var myPop = myArray.pop();
    var myShift = myArray.shift();
    myArray.unshift(myPop);
    myArray.push(myShift);
    Result
    myArray = ['e','b','c','d','a'];
    myPop = 'e'
    myShift = 'a'
    array.reverse() 
    Reverses the order of the elements in the array

    Example
    var myArray = ['a','b','c','d','e'];
    myArray.reverse();
    Result
    myArray = ['e','d','c','b','a'];

    array.slice(start, end)
    Create an array from a part of the current array. Beginning at start and end at end.
    array.splice(index, element to remove [, new element]*);
    remove number of element at position index, and insert new element at index. Returning removed elements.

    Example
    var myArray = ['a','b','c','d','e'];
    var mySliceArray = myArray.slice(1,3);
    var myRemoveElement = myArray.splice(1,3,'z','y');
    Result
    myArray = ['a','z','y','e']
    mySliceArray = ['b','c'];
    myRemoveElement = ['b','c','d'];

    array.sort(function(a,b){return a>b}?)
    Sort the array by the provided function.
    The default comparison function is function(a,b){return a>b}.
    Example
    var myArray = [4,3,1,2];
    myArray.sort();
    Result
    myArray = [1,2,3,4];

    No comments:

    Post a Comment