Pages

Thursday, December 30, 2010

Manipulating Array

/**
 * This class provide functionality to manipulate array of 2
 * dimension
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 31, 2010
 * @version : 1.0
 */

/**
 * A constructor service.
 */
function Arr(){

}

/**
 * A method to create a two dimensional array based on two array.
 * The format of the array is [n][2]. The first element is placed in
 * [n][0], while the second element is placed in [n][1]
 *
 * @param f array the first array
 * @param s array the second array
 * @return two dimensional of array
 */
Arr.prototype.twoDim = function(f,s){
    if(f.length!=s.length)
        return 'must same in length';
    var twoDimm = new Array();
    for(i=0;i<f.length;i++){
        twoDimm[i] = new Array();
        twoDimm[i][0] = f[i];
        twoDimm[i][1] = s[i];
    }
    return twoDimm;
};

/**
 * A method to get the first value of the second index
 * operator [n][0]
 *
 * @param data two dimensional of array in format [n][2]
 * @return first one dimensional of array in format[n]
 */
Arr.prototype.getFirst = function(data){
    var first = new Array();
    for(i=0;i<data.length;i++){
        first[i] = data[i][0];
    }
    return first;
};

/**
 * A method to get the second value of the second index 
 * operator [n][1]
 *
 * @param data two dimensional of array in format [n][2]
 * @return second one dimensional of array in format[n]
 */
Arr.prototype.getSecond = function(data){
    var second = new Array();
    for(i=0;i<data.length;i++){
        second[i] = data[i][1];
    }
    return second;
};

No comments:

Post a Comment