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,526,255 Responses

  1. 1win_maOa表示:

    букмекеры кыргызстана http://www.fabc.com.kg .

  2. 1 vin_riSi表示:

    1win казино вход http://www.bbcc.com.kg .

  3. Rodneytus表示:

    dark markets https://github.com/darkwebmarketslinks/darkwebmarkets – tor darknet drug markets onion

  4. Charleskepsy表示:

    prescription drugs online http://xxlmexicanpharm.com/# medication from mexico pharmacy

  5. Rodneytus表示:

    dark web links https://github.com/darkwebmarketslinks/darkwebmarkets – deep web drug markets tor markets links

  6. hojhzg表示:

    点击获取WPS下载,全面支持文字、表格、演示文稿和PDF管理功能,兼容多种文件格式。WPS Office支持云存储、跨平台同步和团队协作,让办公更加高效。适用于学生、职场人士和企业用户,轻松实现高效办公和文件管理。立即下载WPS,畅享智能办公体验!https://www.wps9.com/         rxjbb

  7. Five Things Everybody Does Wrong Concerning Realistic Sex Doll Silicone life size realistic sex doll

  8. MichaelGeony表示:

    pharmacy coupons https://xxlmexicanpharm.com/# xxl mexican pharm

  9. Davidmoits表示:

    xxl mexican pharm: xxl mexican pharm – xxl mexican pharm

  10. 1win_swOa表示:

    ставки кыргызстан [url=http://fabc.com.kg]http://fabc.com.kg[/url] .

  11. MichaelGeony表示:

    cheapest pharmacy prescription drugs https://xxlmexicanpharm.com/# xxl mexican pharm

  12. mostbet_aopa表示:

    мостбет приложение http://www.gtrtt.com.kg/ .

  13. Guide To African Grey Parrots Sale: The Intermediate Guide For African Grey
    Parrots Sale african grey parrots sale

  14. Davidmoits表示:

    easy canadian pharm: ed meds online canada – easy canadian pharm

  15. cannabis表示:

    Very good info. Lucky me I recently found your blog by chance (stumbleupon).
    I’ve book-marked it for later!

  16. Great post. I used to be checking constantly this weblog and I
    am impressed! Extremely helpful information specifically the final
    phase 🙂 I handle such info much. I used to be seeking this certain information for a very lengthy
    time. Thanks and best of luck.

  17. Barry表示:

    Guide To Pram Bags: The Intermediate Guide For Pram Bags pram bags
    (Barry)

  18. 1 vin_tkSi表示:

    1вин официальный https://www.bbcc.com.kg .

  19. 1 vin_khSi表示:

    1вин вход в личный кабинет http://www.bbcc.com.kg .

  20. WillieRap表示:

    canadian drug pharmacy: canadian pharmacy sarasota – easy canadian pharm

  21. Outstanding story there. What occurred after? Take care!

  22. Davidmoits表示:

    reputable indian pharmacies: Mega India Pharm – MegaIndiaPharm

發佈留言

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