您可以使用 ES6 中的模板字符串和数组的 map() 方法来将 n 个数据分成 n 行代码。以下是一个示例代码:

const data = [1, 2, 3, 4, 5];
const code = data.map(item => `console.log('${item}');`).join('\n');

console.log(code);

在上面的代码中,我们首先定义了一个数组 data,其中包含 5 个数字。然后,我们使用数组的 map() 方法将每个数字转换为一个字符串,这个字符串包含了 console.log() 语句,并使用模板字符串将数字插入到 console.log() 语句中。

最后,我们使用数组的 join() 方法将所有字符串连接起来,并使用换行符 \n 将它们分成 n 行代码。最终的结果将被保存在变量 code 中,并通过 console.log() 打印出来。

JavaScript 将数组数据分割成 N 行代码

原文地址: https://www.cveoy.top/t/topic/ogaL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录