How to serialize form data into an object with nested properties

Given the following inputs:

<input name="person[1]['first']" /> <input name="person[2]['first']" /> <input name="person[3]['first']" />

I want to serialize this into an object like so:

person = { 1: {first:value}, 2: {first:value}, 3: {first:value} }

Is this functionality available in jQuery or javascript now? or will I have to write a function to do it?

-------------Problems Reply------------

You can use, when it is inside <form> tag:

$(formElement).serialize();

You're looking for serializeArray()

EDIT

Adding short example on a form submit:

$('#container').on('submit', '#myForm', function(e) {
e.preventDefault();
var data = $(this).serializeArray(); // $(this) contains the form element
console.log(data); // will output serialized data
console.log(data.email); // will output email input value (if any)
});

Category:javascript Views:4 Time:2019-03-03

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.204 (s). 11 q(s)