使用 javascript 创建虚拟文本

Tam*_*amn 0 javascript

我想使用 javascript 创建 1000 个单词的虚拟文本。我的想法是使用这样的数组

var words =["The sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", ".", "All", "this happened", "more or less","." ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story","." ,"It", "was", "a pleasure", "to", "burn"];
Run Code Online (Sandbox Code Playgroud)

然后使用javascript随机输出1000个单词。我将如何使用 javascript 做到这一点?还有其他方法吗?

San*_*dez 5

看这个

var words =["The sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", ".", "All", "this happened", "more or less","." ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story","." ,"It", "was", "a pleasure", "to", "burn"];
var text = [];
var x = 1000;
while(--x) text.push(words[Math.floor(Math.random() * words.length)]);
document.write(text.join(" "))
Run Code Online (Sandbox Code Playgroud)

参考: SO | 从数组中获取随机值