【发布时间】:2019-10-15 13:04:29
【问题描述】:
我正在尝试找出一种方法来比较两个绝对(ish!)文件位置,并以尽可能最短的方式返回从一个到另一个的相对路径。
/*
Example 1:
..\root\folder\subFolder\myCurrent.file
..\root\folder\subFolder\img\myTarget.image
Expected result:
.\img\myTarget.image
Example 2:
..\root\folder\subFolder\myCurrent.file
..\root\folder\otherSubFolder\img\myTarget.image
Expected result:
..\otherSubFolder\img\myTarget.image
Example 3:
..\root\folder\subFolder\myCurrent.file
..\root\folder\subFolder\myTarget.image
Expected result:
myTarget.image
*/
我尝试将路径拆分为数组并比较长度和值,但结果完全是一团糟,我什至还没有做到...
const currentFilePath = activepath.split('\\')
const currentDir = currentFilePath[currentFilePath.indexOf(currentFilePath[currentFilePath.length - 2])];
const targetFilePath = file.path.split('\\');
const targetDir = targetFilePath[targetFilePath.indexOf(targetFilePath[targetFilePath.length - 2])];
const currentFileDepth = currentFilePath.length;
// and so on...
我想要一个体面、干净的方法来解决这个问题...
【问题讨论】:
标签: javascript path