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.

7
  • 1
    The format you need isn't a valid JavaScript object. Commented Aug 13, 2014 at 13:39
  • I guess I should clarify that the problem is converting the array into a JSON object, sorry for the confusion. Commented Aug 13, 2014 at 14:13
  • There is no such thing as a "JSON object" JSON is a data format, which happens to be based on JavaScript's objects' syntax. Commented Aug 13, 2014 at 14:15
  • An object in JSON format? Commented Aug 13, 2014 at 14:16
  • stackoverflow.com/a/3975890/1835379 Commented Aug 13, 2014 at 14:23

1 Answer 1

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];
});
Sign up to request clarification or add additional context in comments.

Comments

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.