2012-06-27 128 views
3

塊一個逗號分隔的字符串,所以我有一個字符串,它看起來像這樣:劈裂的40

123,532,0302,1234,等等等等(和它的推移而上,有時超過500)。但是,我想將逗號分隔列表分成40個數組,僅此而已。類似PHP中的array_chunk(但是有數組)。

達到此目的的最佳方法是什麼?

+0

目前還不清楚您是希望結果是多個40個項目數組還是多個字符串,其中每個字符串都有40個以逗號分隔的項目。我的答案下面提供了兩種可能性,因爲我不確定你的問題是你想要的結果類型。 – jfriend00

回答

4
String.prototype.chunk = function(n) { 
if (typeof n=='undefined') n=2; 
return this.match(RegExp('.{1,'+n+'}','g')); 
}; 

使用範例:

var s = 'abcdefghijklmnopqrstuvwxyz1234'; 
var a = s.chunk(6); 

產量:

var a = ['abcdef','ghijkl','mnopqr','stuvwx','yz1234']; 

http://javascript.about.com/library/blchunk.htm

編輯次數:我知道我的第一個回答沒有回答這個問題,所以請看這裏:

String.prototype.chunkArr = function (length) { 
    var data = this.split(","); 
    var result = Array(); 
    while(data.length > 0) { 
     result.push(
      data.splice(0,length) 
      .join(",") // comment this or remove line out if you don't want it to re-join into a CSV list of 40 items 
     ); 
    } 
    return result; 
} 

示例用法:

myData = "1,2,3,2,4,5,1,5,1,23,1,23,12,3,12,3,12,3,12,3,12,3,12"; 
console.log(myData.chunkArr(2)); // Change 2 to 40 if you have longer data 

收率:

myData = ["1,2", "3,2", "4,5", "1,5", "1,23", "1,23", "12,3", "12,3", "12,3", "12,3", "12,3", "12"] 

如果註釋或在上述chunkArr功能除去.join(",")部分(大約第7行),則該函數產生:

myData = [ 
    ["1", "2"], 
    ["3", "2"], 
    ["4", "5"], 
    ["1", "5"], 
    ["1", "23"], 
    ["1", "23"], 
    //... and so on 
] 

是的,我知道我可以剛剛添加第二個參數來改變'模式'..但懶惰隨之而來:)

+0

我看到了,唯一的問題是,我不希望它由字符塊,我希望它抓住列表上的40個項目的負載和分裂它們。 –

+0

啊好吧,我編輯我的答案來回答你的問題..這也是更簡單的使用'splice'而不是另一個內部循環。 –

0

如果我正確理解所需的輸出,你可以拆分所有的逗號,然後打破所得長陣列分成若干陣列的不超過40

function splitChunkIntoArrays(data, chunkLength) { 
    var temp = data.split(","); 
    var output = []; 
    var item = []; 
    for (var i = 0; i < temp.length; i++) { 
     // if item is now full, put it into the result and start a new chunk 
     if (item.length == chunkLength) { 
      output.push(item); 
      item = []; 
     } 
     item.push(temp[i]); 
    } 
    // if anything in the last chunk, put it in the result 
    if (item.length != 0) { 
     output.push(item); 
    } 
    return(output); 
} 

// sample output from splitChunkIntoArrays: 
[ 
    [1,2,3,4,..40], 
    [41,42,43,...80], 
    [81,82,83,...120], 
    [121,123,124] 
] 

這會給你數組的數組,其中每個內陣列比chunkLength項長不再。

如果你想要得到的40項長片仍然是字符串(的東西,這不是你的問題完全清楚),你可以再加入內部陣列再次:

function splitChunkIntoStrings(data, chunkLength) { 
    var splitData = splitChunkIntoArrays(data, chunkLength); 
    var results = []; 
    for (var i = 0; i < splitData.length i++) { 
     results.push(splitData[i].join(",")); 
    } 
    return(results); 
} 

// sample output from splitChunkIntoStrings: 
[ 
    "1,2,3,4,..40", 
    "41,42,43,...80", 
    "81,82,83,...120", 
    "121,123,124" 
] 

這將導致一個字符串數組,每個字符串有40個或更少的逗號分隔值。


而且,這裏是一個有點使用數組splice()更精簡的第一個版本功能:

function splitChunkIntoArrays(data, chunkLength) { 
    var temp = data.split(","), result = []; 
    while (temp.length > 0) { 
     result.push(temp.splice(0, chunkLength)); 
    } 
    return(result); 
} 

,或者如果你想要的結果仍然是字符串,你可以這樣做:

function splitChunkIntoStrings(data, chunkLength) { 
    var temp = data.split(","), result = []; 
    while (temp.length > 0) { 
     result.push(temp.splice(0, chunkLength).join(",")); 
    } 
    return(result); 
} 
+0

我該如何在函數中增加這個功能?我希望它返回基於數組的逗號分隔字符串數組。 –

+0

我的第二個代碼塊創建了一個由逗號分隔的字符串組成的數組。第一個代碼塊創建一個數組數組(每個內部數組不超過一個chunkSize)。那些不是你需要的東西? – jfriend00

+0

我爲每個函數添加了示例輸出,以便您可以看到它們每個返回的內容。 – jfriend00