将数组推入 madrill JSON 数组

Push to arrays into madrill JSON array

我有一个表单,其中包含收件人和电子邮件字段。用户可以选择添加更多收件人。

<div class=" form-inline">
    <label for="exampleInputEmail1">Your Recipients</label>
    <script>
        var counter = 1;
var limit = 50;
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML =  "<br>  <input  class='form-control reciptest' placeholder='Recipient Name' type='text'> &nbsp;&nbsp; &nbsp;&nbsp;<input  class='form-control emailtest' placeholder='Recipient Email' type='text'>"
          document.getElementById(divName).appendChild(newdiv);
          counter++;
     }
}
    </script>
    <div id="dynamicInput">
  <input  class='form-control reciptest' placeholder='Recipient Name'  type='text'> <input type="text" class="form-control emailtest" placeholder="Recipient Email">
    <br>
       </div>
       <input type="button" class="btn btn-sm btn-inverse" value="Add Another Email" onClick="addInput('dynamicInput');">

  </div>

在电子邮件处理函数内部,两个 类 的输入被拆分成这样的数组

var emails = document.getElementsByClassName( 'emailtest' ),
    email  = [].map.call(emails, function( email ) {
        return email.value;
    }).join( ',' );

var names = document.getElementsByClassName( 'reciptest' ),
    rname  = [].map.call(names, function( rname ) {
        return rname.value;
    }).join( ',' ); 

这是我的问题所在

此代码目前可以将电子邮件推送到数组中,但我无法将收件人电子邮件添加到数组中

var to =[]; 
email.split(',').forEach(function(k){ to.push ( {email:k, type:'to'});     
rname.split(',').forEach(function(r){ to.push ( {email:k, type:'to', name:'r'});

如何合并这两个数组并使它们保持输入的顺序?

根据您的代码,您似乎试图将电子邮件和收件人姓名合并到数组 to 中。所以,你可以试试这个:

var to =[]; 
var emails = email.split(',');
var rnames = rname.split(',');
emails.forEach(function(em,i){
    var recipient = {email: em, type: 'to', name: null};
    if (rnames.length>i)
        recipient.name = rnames[i];

    to.push(recipient);
});