2017-08-16 26 views
1

如何在我的字符串末尾調用數組的其餘部分?我聽說過擴散算子(但我不太確定)。事實上,我從數組中得到的索引數目是不確定的(除了前兩個)。調用某個數組的其餘部分

let filePath = "folder/folder/potentialFolder/potentialFolder/.../file.txt"; 
let filePathBackup = filePath.split('/'); 
filePathBackup = `${filePathBackup[0]}/${filePathBackup[1]}/backup/${the_rest_of_my_array}`; 

謝謝你的幫忙!

+0

是'filePath'應該是一個字符串? – TricksfortheWeb

+0

使用'filePath.join('/')' – TricksfortheWeb

+0

是的,它被編輯。抱歉。 – Gouigoui

回答

4

嘗試使用.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}`; 
相關問題