【问题标题】:Use each element of the array in a string to form standardized sentences使用字符串中数组的每个元素组成标准化句子
【发布时间】:2018-07-02 18:34:10
【问题描述】:

我想创建一个与标准化句子相关联的数组。我了解如何创建一个数组来列出一系列数字,但是如何将数组的元素添加到一个句子中?

例如,我想创建 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);

【问题讨论】:

    标签: javascript arrays ecmascript-6 range template-literals


    【解决方案1】:

    生成具体的range(...)后,使用Array.maptemplate 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);

    【讨论】:

      猜你喜欢
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 2019-01-03
      • 2021-08-02
      • 2014-07-23
      相关资源
      最近更新 更多