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,229,937 Responses

  1. Incredible! This blog looks exactly like my old one!

    It’s on a totally different subject but it
    has pretty much the same page layout and design. Superb choice of colors!

  2. Howdy! This post could not be written any better!

    Looking at this article reminds me of my previous roommate!
    He constantly kept talking about this. I most certainly will forward this post to him.
    Fairly certain he’ll have a good read. Many thanks for sharing!

  3. LarryCoell表示:

    matadorbet.bid matadorbet giris matadorbet bid

  4. The Advanced Guide To Sprt Toto 4d 안전놀이터 추천

  5. Maryjo表示:

    9 . What Your Parents Teach You About Key Repair Near Me key
    repair near me (Maryjo)

  6. LarryCoell表示:

    matadorbet giris matadorbet.bid matadorbet bid

  7. LarryCoell表示:

    deneme bonusu veren yeni siteler deneme bonusu veren siteler denemebonusu2026.com deneme bonusu veren siteler denemebonusu2026.com

  8. Derekfar表示:

    deneme bonusu veren siteler mycbet.com: deneme bonusu veren yeni siteler – deneme bonusu veren siteler betturkey betturkey.com

  9. The Benefits Of ADHD Private Diagnosis At The Very Least Once In Your Lifetime private Adhd assessment leeds Cost

  10. The 10 Most Scariest Things About Treadmill For Sale Electric treadmill for Sale electric

  11. LarryCoell表示:

    matadorbet bid matadorbet giris matadorbet giris

  12. Attractive section of content. I just stumbled upon your blog and in accession capital to assert that I acquire actually
    enjoyed account your blog posts. Any way I’ll be subscribing to your
    feeds and even I achievement you access consistently quickly.

  13. The Three Greatest Moments In Peritoneal Mesothelioma Asbestos History mesothelioma lawyers

  14. 15 Best Pinterest Boards To Pin On All Time About Land Rover Discovery 3 Key Fob
    Replacement land rover Discovery key Fob not working

  15. ClydePab表示:

    matadorbet bid: matadorbet.bid – matadorbet

  16. I always spent my half an hour to read this website’s
    articles all the time along with a cup of coffee.

    my blog post ads monetization platforms

  17. Thank you for any other great post. Where else may just anyone get that type
    of info in such a perfect means of writing? I’ve a presentation subsequent week, and I am at the look
    for such information.

    My site … led desk light

  18. Hi there very cool website!! Guy .. Excellent ..

    Amazing .. I’ll bookmark your blog and take the feeds also?
    I am satisfied to seek out a lot of useful information right here
    within the put up, we need develop more techniques on this regard, thank you for sharing.
    . . . . .

  19. Тут можно преобрести купить шкаф для ружья шкаф оружейный

  20. Wow that was odd. I just wrote an really long comment but after I
    clicked submit my comment didn’t show up. Grrrr… well I’m
    not writing all that over again. Anyways, just wanted to say fantastic blog!

  21. This is my first time pay a visit at here and i am actually happy to read everthing
    at alone place.

  22. ArnPaish表示:

    What are the top five benefits of using stromectol dose per kg at cheap prices after comparing offers

  23. Good info. Lucky me I discovered your website by accident (stumbleupon).

    I’ve bookmarked it for later!

  24. Derekfar表示:

    matadorbet giris: matadorbet bid – matadorbet bid

發佈留言

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