简体   繁体   English

ES6对多个阵列运行函数的方式有所不同

[英]ES6 run a function differently for multiple arrays

I have a sendEmail function that needs to send different emails based on where the user is placed in the array. 我有一个sendEmail函数,该函数需要根据用户在阵列中的位置发送不同的电子邮件。 For example, I have the following data: 例如,我有以下数据:

[ 
  { download: 'Release sale. 50% off!',
    user: 'test1@gmail.com' },
  { download: 'Release sale. 50% off!',
    user: 'test2@gmail.com' },
  { download: 'Release sale. 50% off!',
    user: 'test3@gmail.com' },
  { download: 'Release sale. 50% off!',
    user: 'test4@gmail.com' } 
]
[ 
  { download: 'Test',
    user: 'test5@gmail.com' 
  } 
]

For each array I need to accumulate all the user emails and the download string and run the following function: 对于每个阵列,我需要累积所有user电子邮件和download字符串并运行以下功能:

await transporter.sendMail({
    from: '"Test" <noreply@test.com>',
    to: [email array here],
    subject: "Here is your file",
    text: `Here is your download: ${download}`
  })

This could be simply achieved by a reduce 这可以简单地通过减少

const emailReduced = [ 
  { download: 'Release sale. 50% off!',
    user: 'test1@gmail.com' },
  { download: 'Release sale. 50% off!',
    user: 'test2@gmail.com' },
  { download: 'Release sale. 50% off!',
    user: 'test3@gmail.com' },
  { download: 'Release sale. 50% off!',
    user: 'test4@gmail.com' } 
].reduce((acc,{download, user}) => {
  return {
    ...acc,
    to: [...acc.to, user],
    text: `Here is your download: ${download}`
  }
}, {from: 'test <test@noreply.com>', subject: 'here is your file', to:[]})

await transporter.sendMail(emailReduced)

You can combine your arrays into one array and iterate over it, calling your sendMail() method for each element. 您可以将数组合并为一个数组并对其进行迭代,为每个元素调用sendMail()方法。

 const users1 = [{ download: 'Release sale. 50% off!', user: 'test1@gmail.com' }, { download: 'Release sale. 50% off!', user: 'test2@gmail.com' }, { download: 'Release sale. 50% off!', user: 'test3@gmail.com' }, { download: 'Release sale. 50% off!', user: 'test4@gmail.com' } ]; const users2 = [{ download: 'Test', user: 'test5@gmail.com' }]; const allUsers = [...users1, ...users2]; const groupedUsers = allUsers.reduce((acc, u) => { const group = acc.find(x => x.download === u.download); if (group) { group.users.push(u.user); return acc; } return [...acc, { download: u.download, users: [u.user] }]; }, []); console.log(groupedUsers) 

With the groupedUsers list from above, you should be able to send emails to groups of users based on the download property. 通过上面的groupedUsers列表,您应该能够基于download属性将电子邮件发送给用户组。

groupedUsers.forEach(async group => {
  await transporter.sendMail({
    from: '"Test" <noreply@test.com>',
    to: group.users,
    subject: "Here is your file",
    text: `Here is your download: ${group.download}`
  });
});

I used the spread ( ... ) operator to combine the arrays, but you could also use concat() if you'd like. 我使用了spread( ... )运算符组合数组,但是如果您愿意,也可以使用concat() const allUsers = users1.concat(users2);

     var objArr = [ 
       { download: 'Release sale. 50% off!',
         user: 'test1@gmail.com' },
         { download: 'Release sale. 50% off!',
         user: 'test2@gmail.com' },
         { download: 'Release sale. 50% off!',
          user: 'test3@gmail.com' },
          { download: 'Release sale. 50% off!',
         user: 'test4@gmail.com' } 
       ]
      let obj = {}
          // Visit non-inherited enumerable keys
       ObjArr.map((data, idx) => {
          console.log(data.download);
          obj = {...obj, ...data.download}
        });
       Console.log(obj); //it wil have the consolidated list of user email yo needed for first array

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

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