I have a json array after the form submission using formSerialize function like this
[
{"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"}
]
and i need to convert this array like this :
[
{
"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
}
]
}
]
}
]
am trying this code to get the multidimesional array.
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);
}
but am not getting the expected result.Please help someone to sort out this issue
test_monitoring_1:?