制作两个或多个对象并在节点js中旋转或随机使用它们
make two or more objects and use them in rotation or random in node js
我有 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"
};
两个对象之间会轮流使用
.我该怎么做才能做到这一点?
旋转我不确定,但你可以像这样随机化。
您可以像这样创建您的 smtp 详细信息
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];
}
//那么这应该给你不同的对象
console.log(getSmtpDetails())
交替
const data = [ {
//here you have your smtp details
id: 1
}, {
//here you have your smtp details
id: 2
}, {
//here you have your smtp details
id: 3
}, {
//here you have your smtp details
id: 4
}
]
function getSmtpDetails(){
return data[Math.floor(Math.random() * data.length)];
}
console.log(getSmtpDetails())
我有 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"
};
两个对象之间会轮流使用 .我该怎么做才能做到这一点?
旋转我不确定,但你可以像这样随机化。 您可以像这样创建您的 smtp 详细信息
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];
}
//那么这应该给你不同的对象
console.log(getSmtpDetails())
交替
const data = [ {
//here you have your smtp details
id: 1
}, {
//here you have your smtp details
id: 2
}, {
//here you have your smtp details
id: 3
}, {
//here you have your smtp details
id: 4
}
]
function getSmtpDetails(){
return data[Math.floor(Math.random() * data.length)];
}
console.log(getSmtpDetails())