3

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

4
  • please add the wanted result as well. Commented Feb 15, 2017 at 7:48
  • { "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 }] }] } Commented Feb 15, 2017 at 7:49
  • Edit the question and add the result there. Properly format your question, it helps us figure out the problem @SoumyaSarasan Commented Feb 15, 2017 at 7:54
  • do you need the one behind the name, if a number is not given test_monitoring_1:? Commented Feb 15, 2017 at 8:03

1 Answer 1

3

You could split the name by minus sign and use the first part as role_name and the second part as report_name property of the inner object.

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; }

Sign up to request clarification or add additional context in comments.

6 Comments

I need one more help. this array i need to insert to mongodb using node js. its not inserting correctly. $.ajax({ type: "post", url: "/insert/" + id, dataType: "json", data: result, success: function(res) { } }); am using thihs code to insert
i have no idea of mongo and jquery.
you may have a look here: stackoverflow.com/help/someone-answers and you could ask a new question about the problem with mongodb.
I need one more assistance
[{"report_name":"monitoring", "role":[{"role_name":"client_management","parameter_name":[{"monitoring":"0","test_monitoring_2":"0","test_monitoring_3":"0"}]}, {"role_name":"client_operations","parameter_name":[{"monitoring":"0","test_monitoring_2":"0","test_monitoring_3":"0"}]}], {"role_name":"operator","parameter_name":[{"monitoring":"0","test_monitoring_2":"0","test_monitoring_3":"0"}]}, ":[{"role_name":"financier","parameter_name":[{"monitoring":"0","test_monitoring_2":"0","test_monitoring_3":"0"}]}]}] i need an array like this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.