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,228,504 Responses

  1. Whichever way you go,Check out our how to have sex pages for more tips on protection. オナドール

  2. Ten Windows Seal Replacement That Will Change Your
    Life double glazed Windows Seal replacement

  3. Merrill表示:

    What’s The Current Job Market For 2 Seat Fabric Sofa Professionals
    Like? 2 seat fabric sofa [Merrill]

  4. Derekfar表示:

    ultrabet: ultrabet guncel – ultrabet bonus

  5. A reputable mesothelioma Asbestos Law (https://Willysforsale.Com/Author/Spherebudget97) company
    provides free assessments of your case to determine whether you are eligible
    for compensation. Furthermore, the top mesothelioma lawyers work on a contingent basis.

  6. slot MPO表示:

    Hello there! This article could not be written much better!
    Going through this article reminds me of my previous roommate!

    He always kept preaching about this. I most certainly will send this post to him.
    Pretty sure he’ll have a good read. I appreciate you for sharing!

    Here is my blog :: slot MPO

  7. Hey there! I know this is kind of off topic but I was wondering if you knew where I could find a captcha plugin for
    my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one?
    Thanks a lot!

  8. ClydePab表示:

    deneme bonusu veren yeni siteler: deneme bonusu veren siteler yerliarama.org – deneme bonusu veren siteler mycbet.com

  9. Guide To Car Seat Buggy 2 In 1: The Intermediate Guide For Car Seat Buggy 2 In 1 car seat Buggy 2 In 1 (https://clashofcryptos.trade)

  10. ThePokies104表示:

    Thanks in favor of sharing such a nice thinking, piece of writing
    is pleasant, thats why i have read it completely

  11. Тут можно преобрести сейф пожаростойкий сейфы огнестойкие

  12. 15 Trends That Are Coming Up About Twin Pushchairs Twin Stroller;
    Taurusjune80.Bravejournal.Net,

  13. 7 Easy Tips For Totally Refreshing Your Samsung American Fridge Freezer With Water And Ice
    Dispenser best non plumbed American fridge freezer with ice Dispenser

  14. Derekfar表示:

    ultrabet giris: ultrabet – ultrabet

  15. Heya i’m for the first time here. I came across
    this board and I find It truly useful & it helped me out much.
    I hope to give something back and aid others like you helped me.

  16. Aracelis表示:

    See What Double Glazing Repairers Tricks The Celebs
    Are Using double glazing repairers (Aracelis)

  17. ClydePab表示:

    Casino Siteleri: guvenilir casino siteleri – casino siteleri win

  18. Do You Know How To Explain Best Car Seats Infant To Your Boss best car seats for infants

  19. The Largest Issue That Comes With Small American Fridge Freezers, And How You Can Fix It american Fridge freezer plumbed ice and Water

  20. misted表示:

    10 Things Everybody Has To Say About Manchester
    Double Glazing Manchester Double Glazing misted

發佈留言

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