Search code examples
javascriptjquerynode.jsdatatablesjquery-datatables-editor

Need help parsing jquery datatables editor data


I have a data format that I receive from jquery data tables editor Datatables Editor which looks like the one below and I need to parse it so that I can store it into db but I have not figured out a way of doing so.

 { action: 'edit',
     'data[1][Name]': 'Some Text ',
     'data[1][Rating]': '1',
     'data[1][Division]': 'Some Text '
 } 

What is the best way to parse this form of data using javascript ? The editor library comes with a php library for parsing the data but I am using nodejs for the backend/


Solution

  • If you want to convert data[] into a literal, you could do something like this :

    var prop, fieldName, literal = {};
    for (prop in data) {
        if (prop != 'action') {
            fieldName = prop.match(/\[(.*?)\]/g)[1].replace(/\]|\[/g,'');
            literal[fieldName] = data[prop];
        } 
    }
    

    demo. It will produce a literal like

    {Name: "Some Text ", Rating: "1", Division: "Some Text "}
    

    that can be used to be inserted in a mongodb for example.

    It simply loops through data, extracts each #2 [] and take the content of that bracket as property names to the literal. I do not at all claim this is the best method.