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,237,795 Responses

  1. BrandonSaisy表示:

    buy prescription drugs from india: indian pharmacy purchase online – top 10 online pharmacy in india

  2. BrandonSaisy表示:

    which ed drug is best: cheap meds – natural ed

  3. Williamagric表示:

    ed online pharmacy https://mexicanpharm24.cheap/# medication from mexico pharmacy

  4. Williamagric表示:

    muse for ed https://pharm24.pro/# vitality ed pills

  5. Prams For Sale Tools To Streamline Your Daily Life Prams For Sale Trick Every Person Should Learn prams For Sale

  6. Can I simply just say what a comfort to discover somebody who actually understands what they’re
    talking about on the web. You actually know how to
    bring an issue to light and make it important. A lot more people have to read this
    and understand this side of your story. I can’t believe you aren’t more popular since you
    certainly possess the gift.

  7. Lazrsmq表示:

    Аттестат школы купить официально с упрощенным обучением в Москве

    forumex.forumex.ru/viewtopic.php?f=7&t=184

  8. Lazrzcr表示:

    Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
    aviantrp.moibb.ru/viewtopic.php?f=8&t=248

  9. BrandonSaisy表示:

    reputable mexican pharmacies online: mexican drugs – п»їbest mexican online pharmacies

  10. Diplomi_jwEn表示:

    купить диплом в новошахтинске orik-diploms.ru .

  11. That is a really good tip especially to those fresh to the
    blogosphere. Short but very accurate information… Appreciate your sharing this one.
    A must read post!

  12. LeroyBak表示:

    online pharmacy india indian pharmacy purchase online reputable indian online pharmacy

  13. 15 Startling Facts About 2 In 1 Stroller Car Seat You’ve Never Heard Of Baby Prams 2 In 1

  14. The Top Reasons People Succeed In The 50 50 Fridge Freezer With Water
    Dispenser Industry fridge freezer with ice and water

  15. double表示:

    The One Glass Pane Replacement Mistake Every Newbie Makes double

  16. Ellie表示:

    10 Quick Tips On Asbestos Attorney In Virginia mesothelioma lawyers
    (Ellie)

  17. DevonPhack表示:

    https://pharm24.pro/# ed remedies
    canadian drugs online

  18. Bet303.com is operated by Codex B.V., a company incorporated under the laws of Curaçao with Company Number 160873 and has a valid Certificate of Operation. This Certificate of Operation is subject to the
    National Ordinance on Off shore Games of Hazard.

  19. No one has a right to your body,ロボット セックス just like you don’t have any rights to anyone else’s body. If you don’t want to take part in sexual activities, it’s your right to say no. Sexual consent is a must-have, every time you have sex.

  20. What Is Adhd Assessment And Why Is Everyone Dissing It? How Do You Get Assessed
    For Adhd – https://Privateadhdassessment20515.Yomoblog.Com

  21. DevonPhack表示:

    http://indianpharm24.pro/# online pharmacy india
    buy prescription drugs without doctor

  22. Diplomi_zbEn表示:

    купить диплом кандидата отзыв orik-diploms.ru .

發佈留言

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