2011-12-21 69 views
0

我在數組中有一堆數字,我需要對它們做一些統計。我需要知道數組中有多少個數字。如何在javascript中對數組進行統計分類?

這裏的數組:

myArray = 
[2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 27, 27, 28, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 34, 34, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 40, 40, 40, 41, 41, 42, 42, 42, 42, 42, 42, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 46, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 52, 52, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54, 55, 55, 55, 55, 55, 56, 57, 57, 57, 57, 57, 57, 57, 58, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61, 62, 62, 63, 63, 63, 64, 64, 64, 64, 64, 65, 65, 66, 66, 66, 67, 67, 67, 68, 68, 68, 69, 69, 69, 69, 69, 69, 70, 70, 71, 71, 71, 71, 71, 71, 71, 72, 73, 73, 73, 73, 74, 74, 74, 75, 75, 75, 76, 77, 78, 78, 79, 79, 80, 80, 81, 81, 81, 81, 81, 82, 82, 82, 82, 83, 83, 83, 83, 84, 84, 84, 85, 85, 85, 85, 85, 86, 86, 86, 86, 86, 86, 87, 87, 87, 88, 88, 89, 89, 90, 90, 91, 91, 91, 92, 93, 94, 95, 95, 95, 95, 95, 96, 96, 96, 96, 97, 97, 99, 99, 99, 99, 99, 101, 101, 102, 102, 103, 103, 105, 105, 105, 106, 107, 107, 108, 108, 109, 109, 109, 109, 110, 112, 112, 113, 113, 113, 114, 114, 115, 116, 116, 117, 118, 120, 121, 121, 121, 122, 122, 123, 123, 123, 124, 124, 124, 124, 125, 126, 127, 128, 129, 130, 130, 131, 131, 131, 131, 132, 133, 133, 134, 134, 134, 136, 136, 136, 136, 137, 137, 137, 138, 138, 138, 139, 139, 139, 140, 141, 141, 142, 142, 143, 144, 144, 144, 144, 145, 150, 150, 153, 155, 159, 160, 160, 161, 162, 164, 164, 166, 176, 180, 180, 180, 181, 181, 187, 191, 192, 193, 194, 197, 200, 203, 211, 216, 224, 251, 280, 333] 

下面是我使用的是什麼,通過它目前解析(這是不工作得非常好):

for (var key in myArray){ 
     var obj = myArray[key]; 

     var count = 0; 
     while(obj < 30){ 
      myArrayStats[0] = count; 
      obj++; 
     } 
     while(obj > 30 && obj < 40){ 
      myArrayStats[1] = count; 
      obj++; 
     } 
     //etc.... 
    } 

使用對象文本創建一個新的陣列會更好,更容易使用,但我不知道如何去做。

+0

我猜如果(obj <30)myArrayStats [0] =(myArrayStats [0] || 0)+ 1,則想要更類似 的東西; (obj> = 30 && obj <40)myArrayStats [1] =(myArrayStats [1] || 0)+ 1; }等等無論如何,希望下面的答案能夠解決它。 – 2011-12-21 09:07:17

+0

在R中,我們只需要執行table(cut(myArray,c(0,30,40,Inf))),結果是(可能會被破壞:'(0,30](30,40)( 40,Inf] 113 56326'˛:)你可以在左邊添加-Inf使其值小於零 – 2011-12-21 09:08:16

回答

4

這只是你的工作是否數組排序與否,但它不應該比需要的事實,這是反正按順序排列的任何算法慢得多:

var myArrayStats = []; 
for(var i = myArray.length; i--;) 
    myArrayStats[myArray[i]] = (myArrayStats[myArray[i]] || 0) + 1; 

console.log(myArrayStats[6]); // Outputs 7 
console.log(myArrayStats[10]); // Outputs 5 
console.log(myArrayStats[20]); // Outputs 5 

如果你想要做的此爲原陣列的僅一部分不是使用slice()得到你想要的陣列的部分,然後做同樣的事情,上面陣列上:

var mySubArray = myArray.slice(0,30); 
var myArrayStats = []; 
for(var i = mySubArray.length; i--;) 
    myArrayStats[mySubArray[i]] = (myArrayStats[mySubArray[i]] || 0) + 1; 

console.log(myArrayStats[6]); // Outputs 7 
console.log(myArrayStats[9]); // Outputs 7 
console.log(myArrayStats[10]); // Outputs undefined 
+0

謝謝!這幾乎是我在找的東西。現在,我如何才能找到數字在這些值之間的某些範圍內的項目,例如0-30,30-60和60-90等等,類似於上面我使用的while循環嗎?可以增加30的值嗎? – michael 2011-12-21 07:27:41

+0

@michael I更新我的答案。 – Paulpro 2011-12-21 07:44:49

0

這聽起來像你有等距箱和希望統計每個值有多少個值。由於這個問題被標記爲jQuery,讓我們使用一個實用函數來避免顯式循環顯示另一種做事情的方式。 (我猜PaulPRO的做法是優越的,但。)

function hist(values, min, max, numBins) { 
    var bins = []; 
    var range = max - min; 
    jQuery.each(values, function(i, value) { 
     var bin = Math.floor(numBins * (value - min)/range); 
     bin = Math.min(Math.max(bin, -1), numBins) + 1; 
     bins[bin] = (bins[bin] || 0) + 1; 
    }); 
    return bins; 
} 

我們可以用下面的測試上面的代碼:

function consoleHist(values, min, max, numBins) { 
    var bins = hist(values, min, max, numBins); 
    var step = (max - min)/numBins; 
    jQuery.each(bins, function(i, count) { 
     var lower = (i - 1) * step + min; 
     var upper = lower + step; 
     if (lower < min) { 
      lower = -Infinity; 
     } 
     if (upper > max) { 
      upper = Infinity; 
     } 
     console.log('[' + lower + ', ' + upper + '): ' + (count || 0)); 
    }); 
} 

consoleHist([-10, 0, 11, 29, 30, 59, 60, 1000], 0, 60, 2); 

可生產在控制檯上的輸出如下:

[-Infinity, 0): 1 
[0, 30): 3 
[30, 60): 2 
[60, Infinity): 2 
相關問題