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,445,807 Responses

  1. Diplomi_ciml表示:

    фальшивые дипломы купить 2orik-diploms.ru .

  2. kypit 1s_fcEn表示:

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

  3. CBD backlinks表示:

    Everyone loves it when people come together
    and share ideas. Great site, stick with it!

  4. kypit 1s_quor表示:

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

  5. kypit 1s_toEn表示:

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

  6. Brandonnot表示:

    Indian Cert Pharm: Best Indian pharmacy – IndianCertPharm

  7. kypit 1s_niEn表示:

    программа 1с купить с установкой программа 1с купить с установкой .

  8. Clara Hines表示:

    Your tips on choosing the right lighting for outdoor decks were spot-on! They really enhance the space—find more ideas at deck builder near me

  9. kypit 1s_peEn表示:

    программа 1с купить с установкой http://www.svarog.forum24.ru/?1-0-0-00000330-000-0-0/ .

  10. kypit 1s_ntEn表示:

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

  11. kypit 1s_atEn表示:

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

  12. kypit 1s_miEn表示:

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

  13. It’s remarkable in support of me to have a
    website, which is useful for my know-how. thanks admin

  14. kypit 1s_fnEn表示:

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

  15. Definitely believe that which you stated. Your
    favorite justification seemed to be on the internet the easiest thing to be aware of.
    I say to you, I certainly get annoyed while people think about worries that they plainly don’t know about.
    You managed to hit the nail upon the top as well as defined out the whole thing without having side-effects , people can take a signal.

    Will probably be back to get more. Thanks

  16. Carrolleruck表示:

    https://mexicaneasypharm.com/# Mexican Easy Pharm
    best canadian pharmacy to order from

  17. Pills information. Effects of Drug Abuse.
    lyrica no prescription
    Some news about pills. Get information now.

  18. I have read so many articles or reviews on the topic
    of the blogger lovers but this article is genuinely a good
    article, keep it up.

  19. Carrolleruck表示:

    https://canadianmdpharm.online/# canadadrugpharmacy com
    reliable canadian pharmacy reviews

  20. kypit 1s_smEn表示:

    настроить 1с бухгалтерия цена https://svarog.forum24.ru/?1-0-0-00000330-000-0-0 .

發佈留言

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