如何将serializeArray更改为普通对象

时间:2017-12-01 10:32:39

标签: javascript laravel serialization multipleselection serializearray

我使用serializeArray来获取所有元素,并且我得到像

这样的对象
[{name: "code[1][barcode]", value: "45534"},
{name: "code[1][rf_id]", value: "535353"},
{name: "code[1][serialize]", value: ""},
{name: "code[2][barcode]", value: "45534"},
{name: "code[2][rf_id]", value: "535353"},
{name: "code[2][serialize]", value: ""},
{name: "custodian[]", value: "3"},
{name: "custodian[]", value: "4"},
{name: "custodian[]", value: "5"}]

我想像这样转换它

{
    code:[
            {barcode:"45534",rf_id:"535353",serialize:""},
            {barcode:"45534",rf_id:"535353",serialize:""}
        ],
    custodian: [3,4,5]
}

目前我正在使用此脚本

var x = $('form#acquiredetail').serializeArray();
console.log(x);
var formData = {};
$.each(x, function(i, field){
    if(field.value.trim() != ""){
      formData[field.name] = field.value;
    }
});

输出

虽然我能够正确地获得code的价值,但是在后端/ Laravel,但问题在于custodian,我得到的是最后一个值,

custodian[]:"5"

我该如何解决这个问题。或者更好的解决方案呢?

我的目标是使用简短的公共代码将所有元素值传递给php。如果有任何替代方法,请建议

2 个答案:

答案 0 :(得分:1)

您必须更改序列化以查看元素是否以数组标记[]结尾,并根据元素数重建密钥:

var x = [{name: "code[1][barcode]", value: "45534"},
{name: "code[1][rf_id]", value: "535353"},
{name: "code[1][serialize]", value: ""},
{name: "code[2][barcode]", value: "45534"},
{name: "code[2][rf_id]", value: "535353"},
{name: "code[2][serialize]", value: ""},
{name: "custodian[]", value: "3"},
{name: "custodian[]", value: "4"},
{name: "custodian[]", value: "5"}];


var formData = {};
var formDataArrays = {};

$.each(x, function(i, field){
    if(field.value.trim() != ""){
      if (/\[\]$/.test(field.name)) {
        var fName = field.name.substr(0,field.name.length-2);
        if (!formDataArrays[fName]) {
          formDataArrays[fName] = [];
        }
        formData[fName+"["+formDataArrays[fName].length+"]"] = field.value;
        formDataArrays[fName].push(field.value);
      } else {
        formData[field.name] = field.value;
      }
    }
});

console.info(formData);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:1)

运行代码段并享受:)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var x = 
[{name: "code[1][barcode]", value: "45534"},
{name: "code[1][rf_id]", value: "535353"},
{name: "code[1][serialize]", value: ""},
{name: "code[2][barcode]", value: "45534"},
{name: "code[2][rf_id]", value: "535353"},
{name: "code[2][serialize]", value: ""},
{name: "custodian[]", value: "3"},
{name: "custodian[]", value: "4"},
{name: "custodian[]", value: "5"}];

   var formData = {};
    $.each(x, function(i, field){
    	if(field.value.trim() != ""){
    		if(formData[field.name] != undefined){
    			var val = formData[field.name];
    			if(!Array.isArray(val)){
    				 arr = [val];
    			}
    			arr.push(field.value.trim());
    			formData[field.name] = arr;
    		}else{
    		  formData[field.name] = field.value;
    		}
        }
    });
    console.log(formData );
    </script>