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,129,025 Responses

  1. Oh my goodness! Incredible article dude! Thanks, However I
    am experiencing issues with your RSS. I don’t know
    why I can’t subscribe to it. Is there anybody else having similar RSS issues?

    Anyone who knows the solution will you kindly respond?
    Thanx!!

  2. They have items to market or services to use.

    Also visit my web-site; ceramic pottery wheel classes

  3. Bookmarks表示:

    Let me reveal you in my evaluation of NewULife.

    Also visit my homepage: Bookmarks

  4. Bookmarks表示:

    This vaporizer has a couple of distinct attributes.

    Feel free to surf to my blog … Bookmarks

  5. Unlike other specialists, we value your time.

    Feel free to visit my homepage :: newulife back office

  6. The 10 Most Terrifying Things About Couches Recliner Couches recliner

  7. Drstobeese表示:

    mexitil online pharmacy: boots pharmacy kamagra – roman online pharmacy

  8. Real HGH Rewards (NewULife Hgh Gel Not Evaluated Yet).

    My blog post – rustic wedding venues south texas

  9. bookmarks表示:

    They have products to market or services to provide.

    Feel free to visit my web site – bookmarks

  10. Bookmarks表示:

    There are several causes of infantile convulsions.

    My web blog: Bookmarks

  11. 10 Wrong Answers For Common Multi Fuel Stoves Uk Questions Do You Know The Right
    Ones? Multi-fuel fireplaces

  12. Bookmarks表示:

    In this situation, the life insurance firm’s cash.

    Visit my blog … Bookmarks

  13. Bookmarks表示:

    Authentic HGH Advantages (NewULife Hgh Gel Not Tested Yet).

    My website; Bookmarks

  14. USP human development hormonal agent (somatropin).

    My site :: Import Auto Repair Shop

  15. Allow shoulder orthopedists near me reveal you in my evaluation of NewULife.

  16. Bookmarks表示:

    NewULife Ranks. Invite to my NewUlife Testimonial.

    Feel free to surf to my homepage :: Bookmarks

  17. USP human growth hormonal agent (somatropin).

    My page – volkswagen service center hours

  18. Easydrorbix表示:

    pharmacy rx one legit: rx express pharmacy panama city fl – Careprost

  19. Unlike various other contractors, we value your time.

    Have a look at my webpage – new u life reviews hgh

  20. Bookmarks表示:

    In this instance, the life insurance company’s cash.

    my blog … Bookmarks

  21. USP human growth hormone (somatropin).

    My web site … Custom vinyl windows

  22. Charles表示:

    Chat with certified lawyers up until you’re satisfied.

    Look into my homepage … Bookmarks (Charles)

  23. JoeGed表示:

    Meilleur промокод на деньги в 1хбет vous permettra de recevoir leur excellent bonus de bienvenue allant jusqu’a $130 sur le sport en paris gratuits. Ce bonus de bienvenue est valable un mois. La maniere la plus simple d’obtenir un code promotionnel pour 1xbet est de visiter l’un des sites partenaires. En general, ces ressources publient regulierement des codes frais dans le cadre de leur cooperation constante avec le bookmaker.

  24. A baby can have as several as 100 convulsions a day.

    Take a look at my webpage – infantile spasms symptoms nhs

  25. The vapor and flavor high quality is likewise terrific.

    My site … where can you buy vapes in australia

  26. Easydrorbix表示:

    lorazepam fearpharmacy: generic cialis online pharmacy – online veterinary pharmacy

  27. Mobile vaporizers make use of batteries as a power source.

    Also visit my site :: canadian plant based meat companies

  28. USP human growth hormonal agent (somatropin).

    Here is my site replacement window cost average

發佈留言

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