JSFiddle - React, Tailwind, and code Playground
HTML
<form id="dataform">
<input type="text" name="data[0][name]">
<input type="email" name="data[0][email]">
<input type="tel" name="data[0][phone]">
<input type="text" name="data[1][name]">
<input type="email" name="data[1][email]">
<input type="tel" name="data[1][phone]">
</form>
JavaScript
const dataform_obj = {} // instantiate empty object
// specify regular expression to capture name components
// for example: data[0][email]
const dataform_re = /^([^\[]+)(\[\d+\])(\[[^\]]+\])$/;
// convert form elements into array then loop
[...document.querySelector('#dataform').elements].forEach(function (elem) {
// capture name components
const match = elem.name.match(dataform_re);
// match[1] is first name, match[2] is index, match[3] is last name
// for example, ["data", "0", "email"]
// instantiate sub-objects if do not exist
if ( !dataform_obj[match[1]] ) {
dataform_obj[match[1]] = {};
}
if ( !dataform_obj[match[1]][match[1] + match[2]] ) {
dataform_obj[match[1]][match[1] + match[2]] = {};
}
// assign element value to object
dataform_obj[match[1]][match[1] + match[2]][match[1] + match[3]] = elem.value;
})
console.log(dataform_obj["data"])