以正确的格式从多维Javascript数组创建JSON对象以提交到Rails

时间:2022-11-05 21:29:07

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

嗨伙计我正在尝试创建一个javascript多维数组,如下所示。

$('#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

我需要一个可以提交给rails的数组(使用ajax $ jquery而不是form_for ..)。我相信我需要的格式

[“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?

修改我的javascript以生成正确格式的正确方法是什么?

Thanks in advance.

提前致谢。

1 个解决方案

#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];
});

#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];
});