制作两个或更多对象,并在node js中旋转或随机使用它们

时间:2018-09-23 07:56:19

标签: javascript node.js object random rotation

我有config.js文件包含:

exports.smtp = {
    host: "smtp.mailtrap.io",
    port: "2525",
    auth: true,
    user: "3295b90cbc8837",
    pass: "x"
};

在另一个文件中,我这样得到smtp元素:

var smtp = {
    host: config.smtp.host,
    port: config.smtp.port,
    secure: false
};

我需要创建多个smtp对象并随机选择要使用的对象 。例子:

exports.smtp = {
    host: "smtp.mailtrap.io",
    port: "2525",
    auth: true,
    user: "3295b90cbc8837",
    pass: "x"
};

exports.smtp = {
    host: "smtp.mailtrap.io",
    port: "2525",
    auth: true,
    user: "3295b90cbc8837",
    pass: "x"
};

两个对象之间的旋转 。我该怎么做才能做到这一点?

1 个答案:

答案 0 :(得分:0)

旋转我不确定,但是您可以像这样随机化。 您可以像这样创建您的smtp详细信息

ceil(((aggregateList.size() + 1)*(0.9)))

//然后应该给您不同的对象

n

替代性地

const data = {
'1' : {
    //here you have your smtp details
    id: 1
},
'2' : {
    //here you have your smtp details
    id: 2
},
'3' : {
    //here you have your smtp details
    id: 3
},
'4' : {
    //here you have your smtp details
    id: 4
}
}

function getSmtpDetails(){
  //I am using 4 as i have 4 object. you should use as many as u have
   var id = Math.floor(Math.random() * 4)+1
   return data[id];
 }