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,480,462 Responses

  1. JacobLoody表示:

    http://www.vizitnews.ru — Все об изготовлении кухонь на заказ на сайте vizitnews.

  2. There’s definately a great deal to learn about this topic.

    I really like all the points you have made.

  3. An impressive share! I’ve just forwarded this onto a co-worker who was doing a little
    research on this. And he in fact ordered me dinner due to the fact that I found it for him…

    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending time to talk about
    this topic here on your site.

  4. Diplomi_topt表示:

    купить диплом без предоплаты купить диплом без предоплаты .

  5. Donaldabula表示:

    Buy Viagra online cheap FastPillsForMen Buy generic 100mg Viagra online

  6. We absolutely love your blog and find almost all of your post’s
    to be precisely what I’m looking for. Does one offer
    guest writers to write content in your case? I wouldn’t mind publishing a post
    or elaborating on a few of the subjects you write concerning here.

    Again, awesome web log!

  7. Donaldabula表示:

    buy viagra here FastPillsForMen sildenafil over the counter

  8. op表示:

    Late Night Fun op

  9. Charlesbot表示:

    http://maxpillsformen.com/# cheapest cialis

  10. Great blog here! Also your website loads up fast!
    What host are you using? Can I get your affiliate link to your host?

    I wish my site loaded up as fast as yours lol

  11. Diplomi_xmpt表示:

    купить диплом в асбесте prema-diploms.ru .

  12. Thank you for discussing the magnitude of house inspections! It’s a extreme step that many put out of your mind when purchasing a homestead Realtors Merida Mexico

  13. Grooming isn’t almost looks; it truly affects their general wellness also! Thanks for spreading recognition! dog parlour near me prices

  14. Jiu-jitsu is not just a fighting style; it’s a lifestyle! The area and sociability are amazing. For those wanting to enhance their skills, I advise visiting 10th planet jiu jitsu for some insightful ideas

  15. Diplomi_typt表示:

    купить дипломы русский язык prema-diploms.ru .

  16. memek basah表示:

    Hi, i read your blog occasionally and i own a similar one and i was just wondering if
    you get a lot of spam responses? If so how do you stop it, any plugin or
    anything you can advise? I get so much lately it’s
    driving me insane so any assistance is very much appreciated.

  17. Moving Waldo表示:

    We stumbled over here by a different web page and thought I may as well check things out.
    I like what I see so now i’m following you. Look forward to finding
    out about your web page again.

  18. Five Great Bachelorette Party Games 오피사이트

  19. Charlesbot表示:

    http://maxpillsformen.com/# cialis for sale

  20. Kyle Robbins表示:

    So happy with the service and selection at my recent visit to a reputable Garage door supplier in Frisco Dick Does Doors – garage door installation

  21. BrettTIT表示:

    Buy generic 100mg Viagra online: cheap viagra – Generic Viagra online

  22. Cory Marsh表示:

    La energía solar es el futuro. Me encanta cómo reduce la huella de carbono solar panels torremolinos

發佈留言

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