Create nested object from inputs

I would like to create a nested object like below base on input forms. First key would be the name attribute, inside it would get required: true if the onput contains reqiuder attribute etc. I assume I need a nested foreach for that, I'm not sure. I added the code what I tried so far.

This is the object I like to create:

var obj = {
    'email.address': {
        required: true,
        email: true
     },
     'first.name': {
         required: false
     },
     'last.name': {
         required: true
     }
};

This is the code I have at the moment:

var formInputs = $('input'),
    obj = {};

$.each(formInputs, function(key, value) {
  obj[this.name] = value.name;
});

console.log(obj)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <input type="email" name="email.address" placeholder="Email address" required />
  <input type="text" name="first.name" placeholder="First name" />
  <input type="text" name="last.name" placeholder="Last name" required />
</form>



Read more here: https://stackoverflow.com/questions/66342423/create-nested-object-from-inputs

Content Attribution

This content was originally published by levipadre at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: