我有一个基于每个英文字母索引的引用数组:
let reference = [0, 2, 3];
以及一系列单词甚至短语:
let words = ["A", "C", "D"];
我想加入words数组
中的每个单词来创建一个简单的句子,但考虑到单词之间的间隙(只要引用数组中的值不是连续的数字!),所以所需的输出将是:
A...C D // gaps filled with three dots
问题是我找不到使用 join()
方法的解决方案!
任何帮助将不胜感激
最佳答案
您可以根据reference
中的空白,从words
创建一个新数组,然后加入:
let reference = [0, 2, 3];
let words = ["A", "C", "D"];
let res = [];
reference.forEach((n, i) => {
if (n - reference[i - 1] >= 2) res.push('...', words[i]);
else res.push(words[i]);
});
console.log(res.join(' '));
关于javascript - 动态使用连接方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58290003/