gpt4 book ai didi

javascript - 在 chalk.js 中的 forEach() 中获取随机颜色

转载 作者:行者123 更新时间:2023-12-01 00:06:23 26 4
gpt4 key购买 nike

我正在运行 forEach() 循环,并且需要 console.log()。但我想为每次迭代获得不同的颜色。我浏览了文档,但找不到任何东西。有什么办法可以达到同样的效果吗?

let arr = ["a", "ab, "abc"]
arr.forEach(arr, e => {
console.log(chalk.red(e)) //maybe something like - chalk.randColor()
})

最佳答案

你可以尝试这样的事情:

// Create an array of possible colors
const color = ['red', 'green', 'blue', 'magenta', 'cyan', 'gray'];
let arr = ["a", "ab", "abc"]
arr.forEach(arr, e => {
// and get a random color name from the array
// and call the function on it
console.log(chalk[color[Math.floor(Math.random() * color.length)]](e))
})

关于javascript - 在 chalk.js 中的 forEach() 中获取随机颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60350038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com