Anonymous
Anonymous

Reputation: 91

How to add a set of object into a single array?

I'm inserting a list of email recipient with same subject and message but different recipients.

I've already tried some method shown below, it get's the number of recipients to be sent, but it for the recipient it only get's the last recipient pushed into the array, that's why it was sent to the same recipient 3 times.

        var emailsObj = [];
        var addEmailObj = [], obj;
        for (i=0 ; i<rowData.length; i++) {
            addEmailObj["claimNo"] = $('#motorClaimNoInfo').val().trim().toUpperCase();
            addEmailObj["fileNo"] = $('#motorClaimNoInfo').attr('fileNo');
            addEmailObj["claimDate"] = $('#motorClaimNoInfo').attr('claimDate');
            addEmailObj["senderCd"] = $('#motorClaimFileComposeEmailFrom').attr('username');
            addEmailObj["sender"] = $('#motorClaimFileComposeEmailFrom').val();
            addEmailObj["senderEmail"] = $('#motorClaimFileComposeEmailFrom').attr('userEmail');
            addEmailObj["recipientCd"] = rowData[i].username;
            addEmailObj["recipient"] = rowData[i].userFullName;
            addEmailObj["recipientEmail"] = rowData[i].userEmail;
            addEmailObj["subject"] = $('#motorClaimFileComposeEmailSubj').val().trim().toUpperCase();
            addEmailObj["message"] = $('#motorClaimFileComposeEmailMsg').val().trim().toUpperCase();
            emailObj.push(addEmailObj[i]);
            emailsObj.push(rowData[i].username);
        }

Here's the console for this code
```[INFO ] 2019-05-06 14:25:37.766 [http-nio-8080-exec-10] MotorClaimInquiryController - emailObj {claimNo=100161002000351, fileNo=1, claimDate=11-21-2016, senderCd=MGSIMBIL, sender=MICHELLE G. SIMBILLO, [email protected], recipientCd=ABSERNIO, recipient=SERNIO, ANNABEL BENAS, [email protected], subject=100161002000351-01, message=}
[INFO ] 2019-05-06 14:25:39.059 [http-nio-8080-exec-10] MotorClaimInquiryController - emailObj {claimNo=100161002000351, fileNo=1, claimDate=11-21-2016, senderCd=MGSIMBIL, sender=MICHELLE G. SIMBILLO, [email protected], recipientCd=ABSERNIO, recipient=SERNIO, ANNABEL BENAS, [email protected], subject=100161002000351-01, message=}
[INFO ] 2019-05-06 14:25:40.578 [http-nio-8080-exec-10] MotorClaimInquiryController - emailObj {claimNo=100161002000351, fileNo=1, claimDate=11-21-2016, senderCd=MGSIMBIL, sender=MICHELLE G. SIMBILLO, [email protected], recipientCd=ABSERNIO, recipient=SERNIO, ANNABEL BENAS, [email protected], subject=100161002000351-01, message=}

The result should have different recipients. Thanks

Upvotes: 1

Views: 62

Answers (2)

Cross Coder
Cross Coder

Reputation: 1

               // let emailsObj = [];
            let addEmailObj = [];
            for (i=0 ; i<rowData.length; i++) {

    let new_user = {
        'claimNo':$('#motorClaimNoInfo').val().trim().toUpperCase(),
        'fileNo':$('#motorClaimNoInfo').attr('fileNo'),
        'claimDate':$('#motorClaimNoInfo').attr('claimDate'),
        'senderCd':$('#motorClaimFileComposeEmailFrom').attr('username'),
        'sender':$('#motorClaimFileComposeEmailFrom').val(),
        'senderEmail':$('#motorClaimFileComposeEmailFrom').attr('userEmail'),
        'recipientCd':rowData[i].username,
        'recipient':rowData[i].userFullName,
        'recipientEmail':rowData[i].userEmail,
        'subject':$('#motorClaimFileComposeEmailSubj').val().trim().toUpperCase(),
        'message':$('#motorClaimFileComposeEmailMsg').val().trim().toUpperCase()

    };
                addEmailObj.push(new_user);
                // emailsObj.push(rowData[i].username);
            }**

>

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 371019

You're only assigning to addEmailObj once:

var addEmailObj = []

This means that every time you mutate or push addEmailObj, you're mutating or pushing the same object. Create it inside the array instead:

for (let i=0 ; i<rowData.length; i++) {
  const addEmailObj = {};

Note that since you're not using it as an array, you shouldn't define it as an array - just define it as a plain object, with {}, not []. (Also, best not to implicitly create global variables - declare your i properly) If at all possible, also declare your variables with ES6 syntax (let and const) rather than ES5 syntax to avoid the confusion that can arise from var's hoisting. (var has function scope, not block scope)

Upvotes: 2

Related Questions