0

Hi Guys I’m trying to create a javascript multidimensional array as seen below.

$('#rfp_import_table > tbody  > tr').each(function(row) {

    TableData[row] =  {
        "sheet_name": $('#sheet_name_' + i).text(),
        "last_row": $('#sheet_last_row_' + i).text(),
        "sheet_id": $('#sheet_id_' + i).text(),
        "sheet_import": $('#sheet_import_' + i).is(':checked'),
        "sheet_import_column": $('#sheet_import_column_' + i).val()
    }
    i++;
});

TableData = $.toJSON(TableData);

This is creating an array that looks like

[
  {
    "sheet_name": "Offeror Instructions",
    "last_row": "99",
    "sheet_id": "0",
    "sheet_import": true,
    "sheet_import_column": "C"
  },
  {
    "sheet_name": "S3 SAI_Availability_Scale",
    "last_row": "22",
    "sheet_id": "38",
    "sheet_import": true,
    "sheet_import_column": "C"
  },
  {
    "sheet_name": "S4 SAI_Deploy_and_Admin",
    "last_row": "21",
    "sheet_id": "39",
    "sheet_import": true,
    "sheet_import_column": "C"
  }
]

I need an array that can be submitted to rails (using ajax $ jquery not form_for..). I beleive the format I need is

[“sheets”:
  {
    "sheet_name": "Offeror Instructions",
    "last_row": "99",
    "sheet_id": "0",
    "sheet_import": true,
    "sheet_import_column": "C"
  },
  {
    "sheet_name": "S3 SAI_Availability_Scale",
    "last_row": "22",
    "sheet_id": "38",
    "sheet_import": true,
    "sheet_import_column": "C"
  },
  {
    "sheet_name": "S4 SAI_Deploy_and_Admin",
    "last_row": "21",
    "sheet_id": "39",
    "sheet_import": true,
    "sheet_import_column": "C"
  }
]

What would be the correct way to modify my javascript to produce the correct format?

Thanks in advance.

pchowdhry
  • 303
  • 5
  • 12

1 Answers1

1
var sampleObj1 = {
                    "a": {
                            "id": "1",
                            "gravatar": "03ce78e04102c67d6144"
                         },
                    "b": {
                            "id": "1",
                            "name": 'asd'
                         },
                    "c": {
                            "id": "1702c3d0-df12-2d1b",
                            "name": "Jeff"
                         }
                    };

var sampleTestArr = Object.keys(sampleObj1).map(function(data){
    return sampleObj1[data];
});
Vasanth
  • 212
  • 1
  • 8