javascriptnode.jsapimailchimpmailchimp-api-v3

How to concatenate two array by using two request in javascript with mailchimp?


The title may not very clear. I'm gonna explain my problem.

Here is my code:

mailchimpMarketing = require("@mailchimp/mailchimp_marketing");

mailchimpMarketing.setConfig({
apiKey: "MY API KEY",
server: "MY SERVER",
});

const getEmailMembersFromList = async (listID, offset , count) => {
  const response = await mailchimpMarketing.lists.getListMembersInfo(listID, {offset, count});
    emails = response.members.map(member => {
      return member.email_address
    })
  console.log(emails)
}

getEmailMembersFromList("MY LIST ID", 0, 1000)
getEmailMembersFromList("MY LIST ID", 1000, 58)

It display in the terminal two arrays of emails addresses. I have to make two calls because there is more than 1000 emails addresses (1058 in total) and the maximum is 1000.

Now I want to concatenate those two array, maybe with the .concat() method but I don't know how to do it in my case.

EDIT 1

mailchimpMarketing = require("@mailchimp/mailchimp_marketing");

mailchimpMarketing.setConfig({
apiKey: "MY API KEY",
server: "MY SERVER",
});

const getEmailMembersFromList = async (listID, offset , count) => {
  const response = await mailchimpMarketing.lists.getListMembersInfo(listID, {offset, count});
    emails = response.members.map(member => {
      return member.email_address
    })
  //console.log(emails)
  return emails
}

getEmailMembersFromList("MY LIST ID", 0, 1000)
getEmailMembersFromList("MY LIST ID", 1000, 58)

Solution

  • Try this code:

    let result = [];
    
    const getEmailMembersFromList = async (listID, offset, count) => {
      await mailchimpMarketing.lists
        .getListMembersInfo(listID, { offset, count })
        .then((response) => {
          emails = response.members.map((member) => {
            return member.email_address;
          });
          // try using spread operator, hopefully it should work
          result = [...result, ...emails];
        });
    };
    
    getEmailMembersFromList("MY LIST ID", 0, 1000);
    getEmailMembersFromList("MY LIST ID", 1000, 58);
    
    console.log(result);