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,147,531 Responses

  1. Penelope表示:

    The Advanced Guide To Medical Malpractice
    Claim medical malpractice lawyers (Penelope)

  2. zeus 138 Demo表示:

    15 Inspiring Facts About Zeus Slot Machines That You Never Knew zeus 138 Demo

  3. 20 Resources That Will Make You More Efficient At Dangerous Drugs Law Firm dangerous drugs attorney

  4. Counter Strike It’s Not As Expensive As You Think counter strike Global Offensive

  5. Making use of for this loan on-line is easy and quick.

  6. 15 Gifts For The Malpractice Attorneys Lover In Your Life malpractice law Firm

  7. A Complete Guide To Online Shopping Uk Electronics Moen Rust-Resistant Shower Rod

  8. undress ai表示:

    Hi, i believe that i saw you visited my web site thus i got here to go back the choose?.I’m
    attempting to to find issues to enhance my web site!I assume its good enough to use a few of
    your concepts!!

  9. kontol表示:

    I think the admin of this web site is actually working
    hard in support of his web page, because here every information is quality
    based material.

  10. 시알리스 구매
    시알리스(Cialis)는 비아그라와 유사한 성기능개선제로, 남성의 발기부전 치료에 사용되는 약물입니다. 시알리스의 주요 활성 성분은 타닐라필(타닐라필로)이라는 화합물입니다.

  11. Franklin表示:

    The Most Effective Double Glazed Window Repair Tricks To Change Your Life window repairs near me (Franklin)

  12. 10 Things That Your Family Taught You About Online Famous Shopping
    Sites online famous shopping sites

  13. Meds information for patients. Effects of Drug Abuse.
    cost generic coreg without a prescription
    Some what you want to know about medication. Get information here.

  14. Diplom_yfKl表示:

    купить диплом кандидата наук http://ukr-diplom.ru/ .

  15. I visited multiple web pages however the audio feature for audio songs existing at
    this web site is genuinely wonderful.

  16. 10 Instagram Accounts On Pinterest To Follow About Boat Accident Compensation boat accident Attorneys

  17. 10 Wrong Answers To Common Window Screen Replacement Questions Do
    You Know The Right Answers? Window Replacement Glass

  18. telegra.ph表示:

    How To Build A Successful New Slots Online When You’re Not Business-Savvy latest slots (telegra.ph)

  19. 3 Ways The Double Glazed Units Near Me Can Influence Your Life Replacement double Glazed Units near Me

  20. Angela表示:

    Three Of The Biggest Catastrophes In Uk Online Phone Shopping Sites The Uk Online Phone Shopping Sites’s 3
    Biggest Disasters In History Relief Kitchen Mat
    [Angela]

  21. Diplom_gbKl表示:

    где купить диплом http://ukr-diplom.ru/ .

  22. Buy Tramadol表示:

    What’s up to all, since I am in fact eager of reading this webpage’s post
    to be updated regularly. It carries fastidious data.

    Visit Tramadol Source – https://rx2go.com/buy/tramadol/

  23. A Productive Rant Concerning Slot Demo Demo Slot x 500

  24. Diplom_gxKl表示:

    купить диплом вуза https://ukr-diplom.ru .

  25. There Are A Few Reasons That People Can Succeed At The High Variance Slots
    Industry slots with bonuses

  26. 비아그라 약국 가격
    비아그라 약국 가격: 비아그라 구매 비용과 가격 비교

發佈留言

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