2016-04-30 34 views
-4

我有一個像這樣的字符串數組。替換字符串數組中的數字javascript

var items= ['products/600/application/286/tabbed/301/text', 
      'products/600/application/286/tabbed/305/text', 
      'products/600/application/500/tabbed/200/text', 
      'products/600/application/500/tabbed/300/text', 
      'products/600/application/286/tinytext', 
      'products/600/differenttext']; 

我需要連續編號更換號碼(相同的數字應該是相同的)和輸出應該是這樣的:

var items= ['products[1]/application[1]/tabbed[1]/text', 
       'products[1]/application[1]/tabbed[2]/text', 
       'products[1]/application[2]/tabbed[1]/text', 
       'products[1]/application[2]/tabbed[2]/text', 
       'products[1]/application[1]/tinytext', 
       'products[1]/differenttext']; 

注意,相同的數字應該得到例如所有286相同的結果應該是[1],因爲它是最低的,500應該替換爲[2],因爲它是下一個更大的垂直。把他們想象成孩子。例如,對於行

products/600/application/286/tabbed/301/text, 
products/600/application/286/tabbed/305/text, 

其同直到tabbed,但在那之後,有2名不同的兒童和第一次的id是301秒是305所以他們應該[1] [2]。現在看行

products/600/application/286/tabbed/305/text, 
products/600/application/500/tabbed/200/text, 
products/600/application/500/tabbed/300/text, 

在這種情況下,應用程序有兩個孩子。 286和500.第二個是500,它有2個孩子。所以500的孩子應該是1和2(200和300)

我申請temp= items[0].match(/\d+/g);後我得到結果[600,286,301]。我是否應該使用json對象來放置從temp獲得的所有結果,或者使用2d數組?

+1

JSON在這裏無關緊要 - 它是一個序列化格式 – Alnitak

+0

這是我不清楚問題是什麼 –

+0

@Ege它將幫助,如果你的示例輸出實際上是正確的...... – Alnitak

回答

3

可以使用map()做這樣的事情replace()

var items = ['products/600/application/286/tabbed/301/text', 
 
    'products/600/application/286/tabbed/305/text', 
 
    'products/600/application/500/tabbed/200/text', 
 
    'products/600/application/500/tabbed/300/text', 
 
    'products/600/application/286/tinytext', 
 
    'products/600/differenttext' 
 
]; 
 
var num = {}; 
 

 
var res = items.map(function(v, i) { // iterate over the array for generating updated array 
 
    return v.replace(/\/(\d+)\/(?:(.*?)\/(\d+)\/(?:(.*?)\/(\d+)\/)?)?/, function(m, m1, m2, m3, m4, m5) { //find the number to replace 
 
    var str = replace(m1, 'main', 0); 
 
    if (m3) { 
 
     str += m2 + replace(m3, m1); 
 
     if (m5) 
 
     str += m4 + replace(m5, m1 + '_' + m3); 
 
    } 
 
    return str; 
 
    }); 
 
}); 
 

 
function replace(mat, ind1) { 
 
    if (!num[ind1]) 
 
    num[ind1] = { 
 
     ind: 0 
 
    }; 
 
    if (num[ind1][mat]) // check index already defined 
 
    return '/[' + num[ind1][mat] + ']/'; // if defined use the previous 
 
    num[ind1][mat] = ++num[ind1].ind ;// else update index in object 
 
    return '/[' + num[ind1].ind + ']/'; // and return the replace string 
 
} 
 

 
document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');


UPDATE: 如果出現次數多於3,那麼你可以做點什麼吧柯本,

var items = ['products/600/application/286/tabbed/301/text/222/kjkjk', 
 
    'products/600/application/286/tabbed/301/text/112/kjkjk', 
 
    'products/600/application/286/tabbed/305/text/222/kjkjk', 
 
    'products/600/application/500/tabbed/200//text/222/kjkjk', 
 
    'products/600/application/500/tabbed/300/text', 
 
    'products/600/application/286/tinytext', 
 
    'products/600/differenttext' 
 
]; 
 
var num = {}; 
 

 
var res = items.map(function(v, i) { // iterate over the array for generating updated array 
 
    var match = 'match'; 
 
    return v.replace(/\/(\d+)\//g, function(m, m1) { 
 
    var res = replace(match, m1); 
 
    match += '_' + m1; 
 
    return res; 
 
    }); 
 
}); 
 
78 
 

 
function replace(ind1, m1) { 
 
    if (!num[ind1]) 
 
    num[ind1] = { 
 
     ind: 0 
 
    }; 
 
    if (!num[ind1][m1]) 
 
    num[ind1][m1] = ++num[ind1].ind; 
 
    return '/[' + num[ind1][m1] + ']/'; 
 
} 
 

 
document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');

+0

好的答案,但我會一直試圖使用地圖來保存number-> index表(併爲表大小單獨索引),而不是具有分期O(n)查找成本的數組。 – Alnitak

+0

@Alnitak:哇,這將是更好,更容易的方式....謝謝...我會更新 –

+0

謝謝你這是一個很好的答案,但你的結果[「產品/ [1] /應用程序/ [2] /標籤/ [3]/text「,」products/[1]/application/[2]/tabbed/[4]/text「,」products/[1]/application/[5]/tabbed/[6]/text 「,」products/[1]/application/[5]/tabbed/[7]/text「,」products/[1]/application/[2]/tinytext「,」products/[1]/differenttext「] 。所以,你得到例如[5]申請後,我需要[1] [2]的應用,因爲有2個不同的數字來代替後 – Ege