Javascript 用于随机化消息的字符串插值数组

Javascript 用于随机化消息的字符串插值数组,javascript,string,ecmascript-6,Javascript,String,Ecmascript 6,我想用一条随机消息欢迎我的用户,但在每一条消息中,它仍然必须引用用户的名字 因此,我想创建一系列流行语,例如: const CATCHPHRASES = [ `Hi ${user}, how you doin' ?`, `Have a great day, ${user} !`, `Sun is shining, so is your day ${user} !` ]; 并有一个函数,给我一个随机的,填写用户作为道具 function getIntroTextRandomiz

我想用一条随机消息欢迎我的用户,但在每一条消息中,它仍然必须引用用户的名字

因此,我想创建一系列流行语,例如:

const CATCHPHRASES = [
   `Hi ${user}, how you doin' ?`,
   `Have a great day, ${user} !`,
   `Sun is shining, so is your day ${user} !`
];
并有一个函数,给我一个随机的,填写用户作为道具

function getIntroTextRandomized(username: string): string {
    // Somehow filling the CATCHPHRASES here with my username props
    return CATCHPHRASES[Math.floor(Math.random() * CATCHPHRASES.length)];
}
我想不出比es6字符串插值更好的解决方案了。
感谢您的帮助

您可以创建一个以
用户
为参数的箭头函数数组。然后,将
用户名
作为参数传递给所选的随机函数

const流行语=[
user=>`Hi${user},你好吗?`,
user=>`祝您有愉快的一天,${user}!`,
user=>`阳光灿烂,你的一天也一样${user}`
];
函数getIntroTextRandomized(用户名){
返回流行语[Math.floor(Math.random()*CATCHPHRASES.length)](用户名);
}

log(getIntroTextRandomized(“john doe”)
您可以创建一个箭头函数数组,它将
用户
作为参数。然后,将
用户名
作为参数传递给所选的随机函数

const流行语=[
user=>`Hi${user},你好吗?`,
user=>`祝您有愉快的一天,${user}!`,
user=>`阳光灿烂,你的一天也一样${user}`
];
函数getIntroTextRandomized(用户名){
返回流行语[Math.floor(Math.random()*CATCHPHRASES.length)](用户名);
}

log(getIntroTextRandomized(“john doe”)
也许您必须使用
替换
函数

const CATCHPHRASES = [
   `Hi :user, how you doin' ?`,
   `Have a great day, :user !`,
   `Sun is shining, so is your day :user !`
];

function getIntroTextRandomized(username: string): string {
    // Somehow filling the CATCHPHRASES here with my username props
    return CATCHPHRASES[Math.floor(Math.random() * CATCHPHRASES.length)].replace(`:user`, username);
}

也许您必须使用
replace
功能

const CATCHPHRASES = [
   `Hi :user, how you doin' ?`,
   `Have a great day, :user !`,
   `Sun is shining, so is your day :user !`
];

function getIntroTextRandomized(username: string): string {
    // Somehow filling the CATCHPHRASES here with my username props
    return CATCHPHRASES[Math.floor(Math.random() * CATCHPHRASES.length)].replace(`:user`, username);
}

使用函数检索短语

const流行语=(用户,索引)=>{
常量短语=[
`你好${user},你好吗?`,
`祝您愉快,${user}!`,
`阳光明媚,你的日子也一样${user}`
];
返回短语[index | | Math.floor(Math.random()*phrases.length)];
};

console.log(流行语('Foo'))使用函数检索短语

const流行语=(用户,索引)=>{
常量短语=[
`你好${user},你好吗?`,
`祝您愉快,${user}!`,
`阳光明媚,你的日子也一样${user}`
];
返回短语[index | | Math.floor(Math.random()*phrases.length)];
};

console.log(流行语('Foo'))是否只替换字符串中的占位符?不是ES6,但它与你所使用的任何语言都是一样的。我想我是想得太多了。只是替换字符串中的占位符?不是ES6,但它与你所使用的任何语言都是一样的。我想我是想得太多了。我想把我的流行语数据排除在这个函数之外。为了使它更易于维护。虽然你接受了唯一不好的答案,但这可能行得通。如果你喜欢的话就用adigas。。。但这里没有正则表达式@LouisLecocqPlease解释为什么这不是正确的答案我想让我的流行语数据远离这个函数。为了使它更易于维护。虽然你接受了唯一不好的答案,但这可能行得通。如果你喜欢的话就用adigas。。。但这里没有正则表达式@请解释一下为什么这不是正确的答案我们有一个赢家。我喜欢数据保持易读性的方式,并且“:user”可以被任何你想让第一眼就容易理解的东西所取代。谢谢我们赢了。我喜欢数据保持易读性的方式,并且“:user”可以被任何你想让第一眼就容易理解的东西所取代。谢谢这是一个很酷的提示。我没有想到这一点。然而,与@anatolii的回答相比,数据集的可读性似乎要差一点,这是一个很酷的提示。我没有想到这一点。然而,对我来说,数据集的可读性似乎比@anatolii的答案要差一点