重新格式化Javascript对象以与Foreach一起使用

时间:2019-04-02 00:34:54

标签: javascript jquery arrays serialization

我有一个具有可重复字段组的表格。我在表单提交中使用serializeArray()来获取所有表单值,但是可重复字段的格式不易使用,因此我需要对其进行修改,以便能够在foreach中使用它们

这是serializeArray()

的回复
{
  "_acffr_repeatable_group_fields": "[\"acrepeater-vehical[0][vehicle-make]\",\"acrepeater-vehical[0][vehicle-model]\",null,\"acrepeater-vehical[0][vehicle-year]\",\"acrepeater-vehical[1][vehicle-make]\",\"acrepeater-vehical[1][vehicle-model]\",null,\"acrepeater-vehical[1][vehicle-year]\",\"acrepeater-vehical[2][vehicle-make]\",\"acrepeater-vehical[2][vehicle-model]\",null,\"acrepeater-vehical[2][vehicle-year]\"]",
  "_acffr_repeatable_groups": "[\"acrepeater-vehical\"]",
  "your-name": "Name",
  "your-email": "test@test.com",
  "YourZipCode": "zip code",
  "acrepeater-vehical[0][vehicle-make]": "Make 1",
  "acrepeater-vehical[0][vehicle-model]": "Model 1",
  "acrepeater-vehical[0][vehicle-type]": "Car",
  "acrepeater-vehical[0][vehicle-year]": "2001",
  "acrepeater-vehical[1][vehicle-make]": "Make 2",
  "acrepeater-vehical[1][vehicle-model]": "Model 2",
  "acrepeater-vehical[1][vehicle-type]": "Truck",
  "acrepeater-vehical[1][vehicle-year]": "2002",
  "acrepeater-vehical[2][vehicle-make]": "Make 3",
  "acrepeater-vehical[2][vehicle-model]": "Model 3",
  "acrepeater-vehical[2][vehicle-type]": "Commercial",
  "acrepeater-vehical[2][vehicle-year]": "2003"
}

从上面可以看到,对于每个重复的字段组,它在操作人员车辆上添加[N],但是这种平面格式不允许我遍历字段集

如何将其重新格式化为适当的格式?

0 个答案:

没有答案