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,448,761 Responses

  1. Responsible For A SEO Backlink Company In London Budget?
    10 Unfortunate Ways To Spend Your Money best seo company in uk

  2. Brandonnot表示:

    reputable mexican pharmacies online: Mexican Easy Pharm – best online pharmacies in mexico

  3. DominicSpurl表示:

    дизайнер интерьера москва недорого дизайнер интерьера стоимость услуг

  4. kypit 1s_dkEn表示:

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

  5. kypit 1s_bhEn表示:

    купить 1с бухгалтерия 8.3 проф версия цена купить 1с бухгалтерия 8.3 проф версия цена .

  6. Hello there! This is kind of off topic but I need some guidance from an established blog.
    Is it difficult to set up your own blog? I’m not very
    techincal but I can figure things out pretty quick.
    I’m thinking about creating my own but I’m not sure where to
    begin. Do you have any tips or suggestions? Many thanks

  7. Brandonnot表示:

    best online canadian pharmacy: reputable canadian online pharmacies – canadian pharmacy online

  8. CurtisBam表示:

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

  9. Lettie Rhodes表示:

    Do you think pressure washing can help prepare a house for sale? I’ll definitely check out Wilson Windows for more information! Power Washing

  10. Brandonnot表示:

    mexican pharmaceuticals online: buying prescription drugs in mexico online – п»їbest mexican online pharmacies

  11. CurtisBam表示:

    mexican rx online: reputable mexican pharmacies online – mexico drug stores pharmacies

  12. kypit 1s_nuEn表示:

    программа 1с купить с установкой [url=http://rodoslav.forum24.ru/?1-3-0-00000090-000-0-0-1737112660]программа 1с купить с установкой[/url] .

  13. Incredible points. Outstanding arguments. Keep up the good effort.

  14. CurtisBam表示:

    Mexican Easy Pharm: Mexican Easy Pharm – mexican drugstore online

  15. memek basah表示:

    It’s appropriate time to make some plans for the long
    run and it is time to be happy. I have learn this publish and if
    I could I want to recommend you few attention-grabbing issues or tips.
    Maybe you can write subsequent articles regarding this article.

    I want to read even more issues approximately it!

  16. you also you will not forget this poker game, if you
    try it at sugar rush casino demo,
    thanks to its extreme 10/10 volatility.

  17. kypit 1s_vfor表示:

    программы фирмы 1с программы фирмы 1с .

  18. For instance, the gentle hum of a coffee shop can enhance creative thinking by providing
    just the right level of ambient noise without overwhelming the senses.

  19. Crypto Slots Casino Techniques To Simplify Your Daily Lifethe One
    Crypto Slots Casino Trick Every Person Should Know crypto slots Casino

  20. wow99表示:

    id=”firstHeading” class=”firstHeading mw-first-heading”>Search results

    Help

    English

    Tools

    Tools
    move to sidebar hide

    Actions

    General

  21. Brandonnot表示:

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

  22. kypit 1s_snEn表示:

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

  23. higo.vn表示:

    Thanks for your marvelous posting! I quite enjoyed reading it, you happen to be a great
    author. I will always bookmark your blog and will come back someday.

    I want to encourage you continue your great writing, have a nice morning!

發佈留言

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