简体   繁体   English

推送到madrill JSON数组中

[英]Push to arrays into madrill JSON array

I have a form which has the fields recipient and email. 我有一个包含收件人和电子邮件字段的表单。 The user has the option to add more recipients. 用户可以选择添加更多收件人。

<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>

Inside the email processing function the inputs of the two classes are split into an array like this 在电子邮件处理功能内部,两个类的输入被分成这样的数组

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( ',' ); 

Here is where my problem lies 这是我的问题所在

This code currently works to push the emails into the array,but I am unable to add the recipients email into the array 该代码当前可以将电子邮件推送到数组中,但是我无法将收件人电子邮件添加到数组中

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'});

How do i go about merging these two arrays and keeping them in the order it was entered ? 我该如何合并这两个数组并使它们保持输入顺序?

According to your code, you seem to try to merge both emails and recipient names in to the array to . 根据您的代码,您似乎尝试将电子邮件和收件人名称都合并到数组to So, you may try this: 因此,您可以尝试以下操作:

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM