Need help parsing jquery datatables editor data

384
November 22, 2016, at 10:03 AM

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/

Answer 1

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.

Rent Charter Buses Company
READ ALSO
Disable browser's autocomplete textboxes in asp.net c#

Disable browser's autocomplete textboxes in asp.net c#

How to disable browser's autocomplete effect on all textboxes in asp. net c# application.

421
Validate Input only on blur or loose focus

Validate Input only on blur or loose focus

I am using - victorjonsson/jQuery-Form-Validator.

363