【发布时间】:2018-12-26 14:14:42
【问题描述】:
在这里,我想在 x 长度之前的最后 4 个字母处添加单词。所以我将字符串转换为数组。应用拆分。但是当我尝试拼接时。在将第二个参数添加到拼接时获取空字符串。寻求帮助。
let x = "abcdefgh";
console.log(x);
console.log(x.length);
let y = x.split('');
console.log(y);
let len = (y.length - 4);
console.log(len);
console.log(y.splice(len));
console.log(y.splice(len, 0, "test"));
【问题讨论】:
-
您的预期输出是什么?您可以在其中访问许多 console.logs
-
请阅读splice 的作用。
-
@PhilippSander 预期输出是 ['a', 'b', 'c', 'd', 'test', 'e', 'f', 'g', 'h' ]请运行上面的代码。你会知道的。
标签: javascript arrays typescript replace splice