2011-08-03 11 views
2

現在のディレクトリに基づいてパスの配列を構築し、あらかじめ定義された最上位ディレクトリに戻す必要があります。 JavaScriptで親パスの配列を構築する

は私が間にあるすべてのパスの配列を必要とする

を2つの変数のtopLevelDirectory 'と「はcurrentDirectory」を持っています。

例:

topLevelDirectory = "/sectionA/sectionB" 
    currentDirectory = "/sectionA/sectionB/sectionC/sectionD/sectionE 

私が値を持つことになり、アレイ 'ALLPATHS' を必要とする:。

allPaths[0] = '/sectionA/sectionB/' 
    allPaths[1] = '/sectionA/sectionB/sectionC/' 
    allPaths[2] = '/sectionA/sectionB/sectionC/sectionD/' 
    allPaths[3] = '/sectionA/sectionB/sectionC/sectionD/sectionE/' 

私はjqueryのを使用しています

私が知っているI currentDirectoryを分割することはできますが、必要な値が得られません。 'sectionA/sectionB/sectionC /'の代わりに 'ectionC'を使用してください。

私は答えに完全なコードが必要ではないと思います。

何か助けていただければ幸いです。

答えて

1

これは非常にエレガントまたはひどく堅牢ではありませんが、あなたの例のために働くようだ...

function segmentPath(topLevelDir, currentDir) { 

    function normalizePath(str) { 
     return str.replace(/(^\/+|\/+$)/g, ''); // strip leading/trailing slashes 
    } 

    topLevelDir = normalizePath(topLevelDir); 
    currentDir = normalizePath(currentDir); 

    var relativePath = normalizePath(currentDir.slice(topLevelDir.length)); 
    relativePath = relativePath.split('/'); 

    var segments = ["/" + topLevelDir]; 

    for (var i = 0, l = relativePath.length; i < l; i++) { 
     segments.push(segments[i] + "/" + relativePath[i]); 
    } 

    return segments; 
} 

がここにデモです:彼らは混乱する可能性があるため、http://jsfiddle.net/QrCBn/

+0

http://jsfiddle.net/PpEH4/がこんなに早く答えてくれてありがとう、私は、1つを受け入れるすべての3つの答えをしようとするつもりです。 – Alex

2

は、正規表現に対する決定初心者に。この問題を解決するためにjQueryを必要としないことを指摘しておく価値があります.JavaScriptはこれを行います。私はあなたが何が起こっているのかを理解することを望んでコードにコメントしました。あなたは満足しているとき、それは

function getAllPaths (topLevelDirectory, currentDirectory) {   

    //split into individual directories 
    var topLevelSegments = topLevelDirectory.split("/"); 
    var currentSegments = currentDirectory.split("/"); 


    //initialise allPaths array with just the top level directory 
    var permutation = topLevelDirectory + "/"; 
    var allPaths = [permutation]; 


    //start appending directories that sit below topLevelDirectory 
    for(i = topLevelSegments.length; i < currentSegments.length; i++) {  
     permutation = permutation + currentSegments[i] + "/"; 
     allPaths.push(permutation); 
    } 
    return allPaths; 
} 

var topLevelDir = "/sectionA/sectionB", 
     currentDir = "/sectionA/sectionB/sectionC/sectionD/sectionE"; 

var allPaths = getAllPaths(topLevelDir, currentDir); 
console.log(allPaths); 

フィドルを働く終わりconsole.logを削除します。

+0

すぐに答えることありがとう、私は3つの答えをすべて試してみるつもりです。 – Alex

1
function getAllPaths(d0, d1) { 
    d0 = (""+d0).replace(/\/$/, ''); 
    d1 = (""+d1).replace(/\/$/, ''); 
    var paths=[d0], ps=d1.slice(d0.length+1).split('/'), i; 
    for (i=1; i<=ps.length; i++) { 
    paths.push(d0 + '/' + ps.slice(0, i).join('/')); 
    } 
    return (d1.indexOf(d0) < 0 && /^\//.test(d0)) ? [] : paths; 
} 

var paths = getAllPaths(topLevelDir, currentDir); 
paths; // ["/sectionA/sectionB", "/sectionA/sectionB/sectionC", "/sectionA/sectionB/sectionC/sectionD", "/sectionA/sectionB/sectionC/sectionD/sectionE"] 
+0

すみません、ありがとう、私は3つの答えをすべて試してみるつもりです。 – Alex

関連する問題