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,638,697 Responses

  1. You’ll Never Guess This Online Shopping Sites Top 7’s Benefits online shopping sites Top 7

  2. 20 Truths About Malpractice Compensation: Busted malpractice Attorney

  3. ” “normal” or “pathological,ラブドール エロ” “good” or “evil” regarding human sexual behavior.

  4. Live Taiwan表示:

    Live Draw Taiwan Selamat Datang di Live Draw Taiwan Lottery,
    di mana kami telah menyediakan Layanan Live Taiwan yang dapat
    dinikmati dengan keamanan dan kelancaran.

  5. well that Maxwell is likely to feel targeted.Although Chloe is never openly antagonistic toward Maxwell,ラブドール 中古

  6. TravauxOnline propose des informations utiles et des conseils pratiques pour vous aider à créer et à entretenir votre jardin.

  7. Nine Things That Your Parent Taught You About Best Online Shopping Sites London best online shopping sites london (https://pullthatcork.com)

  8. Guide To Double Glazed Window Near Me: The Intermediate Guide The Steps To Double Glazed Window Near Me double glazed Window near me

  9. Hudson表示:

    Why Nobody Cares About Truck Accident truck accident lawsuits, Hudson,

  10. Drugs information for patients. What side effects can this medication cause?
    buy cheap singulair pill
    All information about pills. Get information here.

  11. RobertBlica表示:

    https://cenligne.shop/# pharmacie en ligne pas cher

  12. 10 Things You Learned In Preschool That Will Help You With Upvc Windows Repair Repair Upvc Window

  13. 人形表示:

    , and guests arrive in masks, fully dressed—most of the women come in gorgeous, glittering dresses; the men wear suits or tuxedos.ラブドール えろMobile phones,

  14. The SEO Tools Software Mistake That Every Beginner Makes best seo tools software (http://Nitka.By/user/washcolumn06)

  15. so too can practice and rehearsal help our most stressful interpersonal moments.ラブドール おすすめDon’t be afraid to script a difficult conversation,

  16. 人形表示:

    We’re going to determine along with the maker corresponding with availability.Ensure your IP tackle doesn’t have a bad popularity and wasn’t Beforehand blacklisted on the web,人形 セックス

  17. 10-Pinterest Accounts You Should Follow Bmw Replacement Key Cost Bmw replacement key Cost uk

  18. Allenacurf表示:

    Viagra homme prix en pharmacie sans ordonnance: Viagra generique en pharmacie – SildГ©nafil 100mg pharmacie en ligne

  19. Mauricio表示:

    15 Things You Didn’t Know About Slot Demo Gratis demo slot
    mahjong 3 (Mauricio)

  20. Indisputable Proof You Need Online Shopping Sites For Clothes Us Online shopping Sites for clothes

  21. 人形表示:

    and how many more we will probably have in the next 30,人形 エロassuming we hang in there.

  22. Lion4D表示:

    Hi, i think that i saw you visited my web site thus i came to “return the favor”.I
    am attempting to find things to improve my website!I suppose its ok to use a few of your ideas!!

  23. IgnacioSlima表示:

    Sildenafil teva 100 mg sans ordonnance: viagra sans ordonnance – Meilleur Viagra sans ordonnance 24h

  24. 人形表示:

    リアル ドールThe rise of social media has made nearly everything quickly and easily accessible to the public.As it turns out,

  25. A few examples include journaling,practicing breathwork,リアル ドール

  26. RobertBlica表示:

    https://cenligne.com/# pharmacie en ligne sans ordonnance

  27. Are You Responsible For The Nespresso Coffee Machine Magimix Budget?
    10 Terrible Ways To Spend Your Money nespresso machine citiz

  28. it’s likely that you can be helped by having a comrade close by,especially someone you trust and who is a comforting presence.リアル ドール

發佈留言

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