javascriptjquery

How to store data in an array in row/column format using javascript


I am trying to bring all the columns of a table from database and want to store whole data (retrieved from webform) in an array with column names.
For Example Following is my database Table.
StudentName     Roll_No     Subject     Marks
Rock                     101             English      98

I want to store above data in same structure in my array.
Here is my code...

function Insert(FormName, DivName) {
    $.ajax({
        type: "POST",
        url: "" + FormName + ".aspx/GetData",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var Records = xml.find("Table");
            if (Records.length > 0) {
             
                var DetailData = new Array();  // I want to store table in same structure in this array.
                
                $.each(Records, function () {
                    var record = $(this);
                    var ColName = record.find("Column_name").text();   //Getting Column names from database
                    var ColValue = GetColumnsValue(ColName, DivName);  // getting columns value in webform.

                });
            }
        },
        error: function (d) {
            alert(d.responseText);
        }
    });
}

Solution

  • You can use this code

    function Insert(FormName, DivName) {
        $.ajax({
            type: "POST",
            url: "" + FormName + ".aspx/GetData",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var xmlDoc = $.parseXML(response.d);
                var xml = $(xmlDoc);
                var Records = xml.find("Table");
                if (Records.length > 0) {
    
                    var DetailData = []; 
    
                    var rowData = {};
    
                    $.each(Records, function () {
                        var record = $(this);
                        var ColName = record.find("Column_name").text();   
                        var ColValue = GetColumnsValue(ColName, DivName); 
    
                        rowData[ColName] = ColValue;
                    });
    
                    DetailData.push(rowData);
                    console.log(DetailData);
                }
            },
            error: function (d) {
                alert(d.responseText);
            }
        });
    }
    

    After execution, DetailData will look like this:

    [ { "StudentName": "Rock", "Roll_No": "101", "Subject": "English", "Marks": "98" } ]