如何用JavaScript批量生成指定格式语句并输出可复制结果?
批量生成句子并导出复制的方法
你已经用map生成了目标句子数组,接下来可以通过以下方式快速输出并复制:
浏览器控制台场景
如果是在浏览器开发者工具的控制台里运行代码:
- 直接复制到剪贴板:
const traits = ['funny', 'generous', 'smart']; // 替换成你的700个属性数组 const sentences = traits.map(s => `I love you, because you are ${s}`); copy(sentences.join('\n')); // 执行后内容已复制到剪贴板 - 或者打印出来手动选中复制:
执行后控制台会每行显示一个句子,直接选中所有内容复制即可。const traits = ['funny', 'generous', 'smart']; const sentences = traits.map(s => `I love you, because you are ${s}`); console.log(sentences.join('\n'));
Node.js环境场景
如果是在Node.js里运行:
- 打印到终端复制:
const traits = ['funny', 'generous', 'smart']; const sentences = traits.map(s => `I love you, because you are ${s}`); console.log(sentences.join('\n')); - 输出到文件方便后续复制:
执行后会在当前目录生成const fs = require('fs'); const traits = ['funny', 'generous', 'smart']; const sentences = traits.map(s => `I love you, because you are ${s}`); fs.writeFileSync('love_sentences.txt', sentences.join('\n'));love_sentences.txt文件,打开就能复制所有句子。
内容的提问来源于stack exchange,提问作者Dubious Speculation
相关产品推荐
相关产品推荐

