0

I need to implement DataTable struct ,that is in c#, in javascript. For example

function Servers(name)
 {
    this.Name = name;
    this.Columns = new Array(5);

    var rows = new Array(3);

    for (var i=0;i<3;i++)
        rows[i]=new Array(5);

    this.Rows = rows;
 }

I simply access jth element of ith Row by typing like this;

Servers.Rows[i][j]

This works good, but I need to call my object like this;

Servers.Rows[i]["ServerUrl"]

But I dont know how to implement a prototype for this work. Is there anyway to achieve this?

Note: Columns array holds Column names like in c# and Columns array size always equals to Rows' sub array.

2
  • 2
    So you would need to use an Object instead of an Array. rows[i]={}; Commented May 23, 2013 at 13:52
  • Using object is good solution but I set Servers.Rows as array because of response of 3rd party service. Service sends array Commented May 23, 2013 at 14:02

2 Answers 2

2

Live demo

 function create2Array(d1, d2, fn) {
    var arr = [],
        d = function(x, y) {},
        f = fn || d;
    for (var i = 0; i < d1; i++) {
        for (var j = 0, curr = []; j < d2; j++) {
             curr[j] = f.call(window, i, j); 
        };
        arr[i] = curr;
    };
    return arr;
};

function createArrayOfObjects(d1) {
    var arr = [];
    for (var i = 0; i < d1; i++) {
        arr[i] = {};
    };
    return arr;
};



function print2DArray(arr) {
    document.body.innerHTML += "<p><b>Array:</b></p>";
    for (var i = 0, len = arr.length; i< len; i++) {
        document.body.innerHTML += "<p><b>" + i + "</b>: " + arr[i].join(" ") + "</p>";
    };
};


function printArrayOfObj(arr) {
    document.body.innerHTML += "<p><b>Array:</b></p>";
    for (var i = 0, len = arr.length; i< len; i++) {
        document.body.innerHTML += "<p><b>" + i + "</b>: " + JSON.stringify(arr[i]) + "</p>";
    };
};

var Server = {};
Server.Rows = createArrayOfObjects(10);
Server.Rows[0]["something"] = "test";
printArrayOfObj(Server.Rows);

Use it like this:

Server.rows = create2Array(10, 10);

Or you can even specify a custom init function which takes the index as param. Say if you want to init your matrix with 0 by default:

Server.rows = create2Array(10, 10, function(x, y) { return 0;});

Or if you use an object.

Server.rows = create2Array(10, 10);
Server.rows[0]["ServerUrl"] = "test";
1
  • But, I dont know how can I access my variable by calling like this; Server.Rows[0]["ServerUrl"] still. Commented May 23, 2013 at 13:57
0

You should use a hash/object for this.

If you want to refer to variables in a data structure by name/string in javascript the an object is the best option. See here: https://developer.mozilla.org/en/docs/JavaScript/Guide/Working_with_objects

1
  • You should maybe improve your post and explain how. It risks being flagged as "Very low quality" as it stands. Commented May 23, 2013 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.