JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

1,451,096 Responses

  1. Brandonnot表示:

    safe canadian pharmacy: canadian valley pharmacy – canadian pharmacies

  2. kypit 1s_rnor表示:

    1с бухгалтерия купить 1с бухгалтерия купить .

  3. where to get cheap allopurinol without rx can i get generic allopurinol tablets where to get generic allopurinol tablets
    can you get allopurinol without rx can you buy cheap allopurinol for sale where buy generic allopurinol tablets
    order allopurinol without dr prescription
    order cheap allopurinol online can i get cheap allopurinol without rx how can i get cheap allopurinol pills
    can i buy cheap allopurinol pills where to get generic allopurinol without rx where can i get cheap allopurinol tablets

  4. CurtisBam表示:

    Best Indian pharmacy: Indian Cert Pharm – Indian pharmacy that ships to usa

  5. 밤문화表示:

    Sleep Well Tips The Best Way To Get That Goodnight Sleep You’ve Always Wanted 밤문화

  6. Brandonnot表示:

    buying prescription drugs in mexico online: Mexican Easy Pharm – п»їbest mexican online pharmacies

  7. Brandonnot表示:

    medicine in mexico pharmacies: Mexican Easy Pharm – Mexican Easy Pharm

  8. Brandonnot表示:

    canadian drug prices: 77 canadian pharmacy – canadian pharmacy reviews

  9. The Most Pervasive Issues In Car Key Cutting Near Me key cut place near me

  10. Oh my goodness! Incredible article dude! Thank you, However I am having issues with your RSS.
    I don’t know why I cannot subscribe to it. Is there anyone else getting identical RSS problems?
    Anyone that knows the solution will you kindly respond?
    Thanks!!

  11. Truck Lawyers表示:

    You’ll Never Guess This Truck Lawyers Near Me’s Tricks
    Truck Lawyers

  12. CurtisBam表示:

    Indian pharmacy that ships to usa: Indian pharmacy international shipping – Online pharmacy

  13. I visited many sites however the audio feature for audio songs present at
    this website is actually fabulous.

  14. Mat6tube表示:

    Mat6tube Truly enlightening articles or reviews right now

  15. kypit 1s_lvEn表示:

    настройка 1с бухгалтерии цена настройка 1с бухгалтерии цена .

  16. Hi there, its nice paragraph about media print, we all be familiar with
    media is a fantastic source of data.

  17. It’s actually a great and helpful piece of info.
    I’m happy that you simply shared this useful information with us.
    Please keep us informed like this. Thanks for sharing.

  18. It’s difficult to find experienced people about this subject, however, you sound like
    you know what you’re talking about! Thanks

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。