如何在字符串末尾调用数组的其余部分?我听说过传播算子(但我不确定)。确实,我从数组中获得的索引数量是不确定的(除了前两个除外)。
let filePath = "folder/folder/potentialFolder/potentialFolder/.../file.txt";
let filePathBackup = filePath.split('/');
filePathBackup = `${filePathBackup[0]}/${filePathBackup[1]}/backup/${the_rest_of_my_array}`;
谢谢您的帮助 !
最佳答案
尝试像这样使用.slice()
和.join()
:
let filePath = 'folder/folder/potentialFolder/potentialFolder/.../file.txt';
let filePathBackup = filePath.split('/');
let rest = filePathBackup.slice(2).join('/');
let result = ${filePathBackup[0]}/${filePathBackup[1]}/backup/${rest}`;