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,153,437 Responses

  1. The Reasons To Focus On Enhancing Slot Gaming Experience Slot machine reviews

  2. www.mhcf.Net表示:

    15 Things You’ve Never Known About Panels For Upvc Doors upvc doors
    price, http://www.mhcf.Net,

  3. Online Shopping Websites Clothes Techniques To Simplify
    Your Everyday Lifethe Only Online Shopping Websites Clothes
    Trick That Everyone Should Be Able To Online shopping websites Clothes

  4. Williams表示:

    Are You Tired Of New Upvc Door? 10 Inspirational Sources To
    Invigorate Your Love front upvc doors; Williams,

  5. LynwoodbioMs表示:

    Philippe Coutinho Correia https://philippecoutinho.prostoprosport-br.com Brazilian footballer, midfielder of the English club Aston Villa, playing on loan for the Qatari club Al-Duhail. He is known for his vision, passing, dribbling and long-range ability.

  6. Jestine表示:

    “Ask Me Anything,” 10 Responses To Your Questions About Veterans
    Disability Attorney veterans disability lawsuits (Jestine)

  7. Cs2 case表示:

    The Top Reasons People Succeed In The How Many Cases Are
    There In CSGO Industry Cs2 case

  8. Lola表示:

    We stumbled over here from a different web page and thought I may as well check things out.
    I like what I see so now i am following you. Look forward to exploring your web page again.

  9. Shavonne表示:

    5 Common Myths About Truck Wreck Attorney You Should Stay
    Clear Of truck accident (Shavonne)

  10. 10 Things That Your Family Teach You About Window Services London Window Services London

  11. This Week’s Top Stories About Upvc Window Repairs Upvc Window Repairs Near Me

  12. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://farmfactory.dk/bogxt of type and scrambled it to make a type specimen book.

  13. 5 Accident Lawyer Projects For Any Budget Accident Lawyers

  14. TimothyNed表示:

    Erling Breut Haaland https://erling-haaland.prostoprosport-br.com Futebolista noruegues, atacante do clube ingles Manchester City e Selecao da Noruega. Detentor do recorde da Premier League inglesa em gols por temporada.

  15. Why You Should Focus On Improving Play Slots online slot machines, http://www.valleyforge.Org,

  16. 7 Practical Tips For Making The Most Of Your Semi
    Truck Compensation semi truck Accident Attorney

  17. Tommyvom表示:

    Hello there! can i order finasteride great internet site.

  18. TAMPUNG99表示:

    Keep on writing, great job!

  19. THC Syrup表示:

    Ahaa, its good conversation concerning this paragraph at this place at this website, I have read all that, so at this time me also commenting at this place.

  20. this page表示:

    A fascinating discussion is definitely worth comment.
    I do think that you should publish more about this subject, it may not be a taboo subject but generally people do not speak
    about these topics. To the next! Cheers!!

  21. Jere Alas表示:

    7 Simple Tricks To Rolling With Your Double Glazing Windows Repair Jere Alas

  22. Ten Adhd Symptoms Test Myths That Aren’t Always The Truth adhd Symptoms women adult

  23. spankbang表示:

    Keep this going please, great job!

發佈留言

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