使用字符串中数组的每个元素组成标准化的句子
Use each element of the array in a string to form standardized sentences
我想创建一个与标准化句子相关联的数组。我了解如何创建一个数组来列出一系列数字,但是,如何将数组的元素添加到一个句子中?
例如,我想创建 25 个句子,所有句子中的数字都来自我的数组。
句子模板:
This is number: (a number from the array), okay?
句子会是这样的:
This is number **1**, okay?
This is number **2**, okay?
This is number **3**, okay?
...
This is number **25**, okay?
这是我当前的数组代码:
function range(start, end) {
return Array(end - start + 1).fill().map((_, idx) => start + idx)
}
var result = range(1, 25);
console.log(result);
生成特定的 range(...)
后,使用 Array.map 和
template literals 从特定的 num
:
轻松生成标准化句子
function range(start, end) {
return Array(end - start + 1).fill().map((_, idx) => start + idx)
}
const sentences = range(1, 25).map(num => `This is number ${num}, okay?`);
console.log(sentences);
我想创建一个与标准化句子相关联的数组。我了解如何创建一个数组来列出一系列数字,但是,如何将数组的元素添加到一个句子中?
例如,我想创建 25 个句子,所有句子中的数字都来自我的数组。
句子模板:
This is number: (a number from the array), okay?
句子会是这样的:
This is number **1**, okay?
This is number **2**, okay?
This is number **3**, okay?
...
This is number **25**, okay?
这是我当前的数组代码:
function range(start, end) {
return Array(end - start + 1).fill().map((_, idx) => start + idx)
}
var result = range(1, 25);
console.log(result);
生成特定的 range(...)
后,使用 Array.map 和
template literals 从特定的 num
:
function range(start, end) {
return Array(end - start + 1).fill().map((_, idx) => start + idx)
}
const sentences = range(1, 25).map(num => `This is number ${num}, okay?`);
console.log(sentences);