jquery - special case of serializing form to a javascript object -


building upon $.fn.serializeobject() function this question, i'd able support "dot notation" in form names, so:

<form>     <input name="property.items[0].text" value="item 1" />     <input name="property.items[0].value" value="1" />     <input name="property.items[1].text" value="item 2" />     <input name="property.items[1].value" value="2" /> </form> 

given $('form').serializearray() produces following:

[{"name":"property.items[0].text","value":"item 1"},  {"name":"property.items[0].value","value":"1"},  {"name":"property.items[1].text","value":"item 2"},  {"name":"property.items[1].value","value":"2"}] 

how achieve desired result below:

{property: {items: [{text: 'item 1', value: '1'},                     {text: 'item 2', value: '2'}]} } 

any appreciated.

edit: specific, desired code added serializeobject extension in addition way works now, support above convention. here's existing method convenience.

$.fn.serializeobject = function() {     var o = {};     var = this.serializearray();     $.each(a, function() {         if (o[this.name]) {             if (!o[this.name].push) {                 o[this.name] = [o[this.name]];             }             o[this.name].push(this.value || '');         } else {             o[this.name] = this.value || '';         }     });     return o; }; 

edit 2: feeding off answer provided, here's current implementation:

$.fn.serializeobject = function() {   var o = {};   var = this.serializearray();   var regarray = /^([^\[\]]+)\[(\d+)\]$/;    $.each(a, function(i) {       var name = this.name;       var value = this.value;        // let's allow "dot notation" in input names       var props = name.split('.');       var position = o;       while (props.length) {           var key = props.shift();           var matches;           if (matches = regarray.exec(key)) {               var p = matches[1];               var n = matches[2];               if (!position[p]) position[p] = [];               if (!position[p][n]) position[p][n] = {};               position = position[p][n];           } else {               if (!props.length) {                   if (!position[key]) position[key] = value || '';                   else if (position[key]) {                       if (!position[key].push) position[key] = [position[key]];                       position[key].push(value || '');                   }               } else {                   if (!position[key]) position[key] = {};                   position = position[key];               }           }       }   });   return o; }; 

you can see in action here

this solution bit static. should trick:

var serialized = $.fn.serializeobject(),             properties = {},             property = [],             position = {},             key = '',             n = 0,             matchname = '',             = 0;          (i = 0; < serialized.length; += 1) {             property = serialized[i].name.split('.');             position = properties;             while (property.length) {                 key = property.shift();                 if (key.match(/\[\d+\]/) && key.match(/\[\d+\]/).join().match(/\d+/g) ) {                     matchname = key.match(/\w+/)[0]                     n = parseint(key.match(/\[\d+\]/).join().match(/\d+/g), 10);                     if (!position[matchname]) {                         position[matchname] = [];                     }                      if (!position[matchname][n]) {                         position[matchname][n] = {}                     }                      position = position[matchname][n]                 } else {                     if (!property.length) {                         position[key] = serialized[i].value                     } else {                         if (!position[key]) {                             position[key] = {};                         }                         position = position[key]                     }                 }              }         }          console.log(properties); 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -