我在使用formSerialize函数提交表单后有一个json数组,如下所示
[
{"name":"client_management-testmonitoring","value":"0"},
{"name":"client_operations-testmonitoring","value":"0"},
{"name":"tpl_management-testmonitoring","value":"0"},
{"name":"tpl_operations-testmonitoring","value":"0"},
{"name":"channel_partner-testmonitoring","value":"0"},
{"name":"operator-testmonitoring","value":"0"},
{"name":"financier-testmonitoring","value":"0"},
{"name":"client_management-test_monitoring_2","value":"0"},
{"name":"client_operations-test_monitoring_2","value":"0"},
{"name":"tpl_management-test_monitoring_2","value":"0"},
{"name":"tpl_operations-test_monitoring_2","value":"0"},
{"name":"channel_partner-test_monitoring_2","value":"0"},
{"name":"operator-test_monitoring_2","value":"0"},
{"name":"financier-test_monitoring_2","value":"0"},
{"name":"client_management-test_monitoring_3","value":"0"},
{"name":"client_operations-test_monitoring_3","value":"0"},
{"name":"tpl_management-test_monitoring_3","value":"0"},
{"name":"tpl_operations-test_monitoring_3","value":"0"},
{"name":"channel_partner-test_monitoring_3","value":"0"},
{"name":"operator-test_monitoring_3","value":"0"},
{"name":"financier-test_monitoring_3","value":"0"}
]
我需要像这样转换这个数组:
[
{
"role": [
{
"role_name": "client_management",
"report_name": [
{
"test_monitoring_1": 1,
"test_monitoring_2": 1,
"test_monitoring_3": 0
}
]
}
]
},
{
"role": [
{
"role_name": "financier",
"report_name": [
{
"test_monitoring_1": 1,
"test_monitoring_2": 0,
"test_monitoring_3": 1
}
]
}
]
}
]
我正在尝试使用此代码来获取multidimesional数组。
var formData = $('#' + reportType).serializeArray(),matrix_array =[];
for (var u = 0; u < formData.length; u++) {
for (var user in formData[u])
{
if (user == 'name') {
var matrix_name_n = formData[u][user],
matrix_name_a = matrix_name_n.split('-'),
role_name = matrix_name_a[0],
parameter_name = matrix_name_a[1];
var matrix_array_2 = [];
}
if (user == 'value') {
var matrix_param_value = formData[u][user], matrix_array_3 = [matrix_param_value];
}
}
var matrix_array2 = {};
var matrix_array2 = {};
matrix_array2["role"] = role_name;
matrix_array2[parameter_name] = matrix_param_value;
matrix_array_2.push(matrix_array2);
matrix_array.push(matrix_array_2);
var insert_matrix = {};
insert_matrix = JSON.stringify(formData);
}
但是没有得到预期的结果。请帮助某人解决这个问题
答案 0 :(得分:3)
您可以使用减号拆分名称,并将第一部分用作role_name
,将第二部分用作内部对象的report_name
属性。
var data = [{ name: "client_management-testmonitoring", value: 0 }, { name: "client_operations-testmonitoring", value: 0 }, { name: "tpl_management-testmonitoring", value: 0 }, { name: "tpl_operations-testmonitoring", value: 0 }, { name: "channel_partner-testmonitoring", value: 0 }, { name: "operator-testmonitoring", value: 0 }, { name: "financier-testmonitoring", value: 0 }, { name: "client_management-test_monitoring_2", value: 0 }, { name: "client_operations-test_monitoring_2", value: 0 }, { name: "tpl_management-test_monitoring_2", value: 0 }, { name: "tpl_operations-test_monitoring_2", value: 0 }, { name: "channel_partner-test_monitoring_2", value: 0 }, { name: "operator-test_monitoring_2", value: 0 }, { name: "financier-test_monitoring_2", value: 0 }, { name: "client_management-test_monitoring_3", value: 0 }, { name: "client_operations-test_monitoring_3", value: 0 }, { name: "tpl_management-test_monitoring_3", value: 0 }, { name: "tpl_operations-test_monitoring_3", value: 0 }, { name: "channel_partner-test_monitoring_3", value: 0 }, { name: "operator-test_monitoring_3", value: 0 }, { name: "financier-test_monitoring_3", value: 0 }],
result = data.reduce(function (hash) {
return function (r, a) {
var parts = a.name.split('-');
if (!hash[parts[0]]) {
hash[parts[0]] = {};
r[0].role.push({ role_name: parts[0], report_name: [hash[parts[0]]] });
}
if (!parts[1].match(/\d$/)) {
parts[1] = 'test_monitoring_1';
}
hash[parts[0]][parts[1]] = a.value;
return r;
}
}(Object.create(null)), [{ report_name: "monitoring", role: [] }]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }