Skip to main content
3 de 3
adicionou 704 caracteres ao conteúdo
Sam
  • 80,8mil
  • 22
  • 73
  • 128

Creio que seja isso que deseja. Fazer um loop pelos forms e criar o objeto com as arrays inserindo os valores dos campos:

var obj = {};

$("form").each(function(i){
   
   var $this = $(this);
   
   if(i == 0){

      form1 = $("[name='nome']", $(this)).val();
      obj[form1] = [{}];

   }else{
      
      obj[form1][0][$this.attr("id")] = [{}];

      $(".linha", $this).each(function(){
      
         obj[form1][0][$this.attr("id")][0][$("[name='dia']", $(this)).val()] = [{
      
               "inicio" : $("[name='inicio']", $(this)).val(),
               "fim" : $("[name='fim']", $(this)).val(),
         
         }]
      });
   }
   
});

console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="titulo">
   <input type="text" name="nome" value="Fulano">
</form>

<form id="turno1">
   <div class="linha">
      <input type="text" name="dia" value="t1 d1">
      <input type="text" name="inicio" value="t1 ini1">
      <input type="text" name="fim" value="t1 fim1">
   </div>
   <div class="linha">
      <input type="text" name="dia" value="t1 d2">
      <input type="text" name="inicio" value="t1 ini2">
      <input type="text" name="fim" value="t1 fim2">
   </div>
</form>

<form id="turno2">
   <div class="linha">
      <input type="text" name="dia" value="t2 d1">
      <input type="text" name="inicio" value="t2 ini1">
      <input type="text" name="fim" value="t2 fim1">
   </div>
   <div class="linha">
      <input type="text" name="dia" value="t2 d2">
      <input type="text" name="inicio" value="t2 ini2">
      <input type="text" name="fim" value="t2 fim2">
   </div>
</form>

Sam
  • 80,8mil
  • 22
  • 73
  • 128