Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Convert form data to JavaScript object with jQuery

$.fn.getForm2obj = function() {
  var _ = {};
  $.map(this.serializeArray(), function(n) {
    const keys = n.name.match(/[a-zA-Z0-9_]+|(?=[])/g);
    if (keys.length > 1) {
      let tmp = _;
      pop = keys.pop();
      for (let i = 0; i < keys.length, j = keys[i]; i++) {
        tmp[j] = (!tmp[j] ? (pop == '') ? [] : {} : tmp[j]), tmp = tmp[j];
      }
      if (pop == '') tmp = (!Array.isArray(tmp) ? [] : tmp), tmp.push(n.value);
      else tmp[pop] = n.value;
    } else _[keys.pop()] = n.value;
  });
  return _;
}

$('form').getForm2obj();
Comment

PREVIOUS NEXT
Code Example
Javascript :: How to Use the Return Keyword in a Function 
Javascript :: python save api response to json file append 
Javascript :: actual jquery 
Javascript :: what is react-instantsearch-dom 
Javascript :: react-spring 
Javascript :: random color javascript 
Javascript :: js rotate matrix 
Javascript :: calculate percentage in javascript 
Javascript :: find parent index of nested array object javascript 
Javascript :: vue compositon api reusable code reuse code 
Javascript :: React Native Component with Random Hexa 
Javascript :: js template literal without white spaces 
Javascript :: Backbone Sync Example 
Javascript :: tiled spatialmaterial godot 
Javascript :: react axios project importing online same products with table from fake API 
Javascript :: convert js to tsx 
Javascript :: convert array to conventional array js 
Javascript :: Component With Both Data And Props 
Javascript :: nextjs youtube embed 
Javascript :: angular routing appcomponent snipped 
Javascript :: OwlCarousel not working after build react js 
Javascript :: move_uploaded_file equivalent in js 
Javascript :: addeve 
Javascript :: deletenode javascript 
Javascript :: translate javascript to english 
Javascript :: add decimal places to number javascript 
Javascript :: how to create a new window with a specifc link jquery 
Javascript :: html5 javascript json vertical colom grap 
Javascript :: folder array randomizer 
Javascript :: Passing JSON to Javascript in Laravel – but JS is converting the JSON to HTML Entities 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =