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,452,182 Responses

  1. kypit 1s_kdor表示:

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

  2. CurtisBam表示:

    best rated canadian pharmacy: Canadian Md Pharm – cheap canadian pharmacy

  3. Anthony Tran表示:

    Thanks for the useful suggestions. Discover more at متن روز پدر برای استوری

  4. CurtisBam表示:

    Online medicine: Indian Cert Pharm – Online medicine

  5. Carlabith表示:

    Ссылка на актуальное рабочее зеркало букмекерской конторы мостбет на сегодняшний день, доступная для посещения прямо сейчас без блокировок и ограничений.

  6. Wondpatex App表示:

    Hi there it’s me, I am also visiting this web site daily, this website is in fact fastidious and the
    people are actually sharing good thoughts.

  7. Brandonnot表示:

    mexican online pharmacies prescription drugs: mexico drug stores pharmacies – mexican online pharmacies prescription drugs

  8. ” It certainly pays off finding reliable sources linked directly back towards trusted organizations executing works efficiently each time whilst ensuring every client walks away satisfied once involved alongside teams operating    # &lt ;/div&gt ; ! Pressure Washing Company

  9. AngelCroft表示:

    fitnessclubmalibu.ru — Онлайн-ресурс для поиска стильных кухонь на заказ.

  10. PENIPU表示:

    When I initially left a comment I seem to have clicked the
    -Notify me when new comments are added-
    checkbox and from now on each time a comment is added I get four emails with the same comment.
    There has to be a way you are able to remove me from that service?
    Cheers!

  11. CurtisBam表示:

    Best Indian pharmacy: Indian pharmacy international shipping – Indian pharmacy that ships to usa

  12. Brandonnot表示:

    canadian online pharmacy: canadian drugstore online – buying from canadian pharmacies

  13. AngelCroft表示:

    http://fitnessclubmalibu.ru — Узнайте больше о возможностях заказа кухонь на заказ.

  14. Brandonnot表示:

    Mexican Easy Pharm: mexican rx online – mexican border pharmacies shipping to usa

  15. buy Macaw表示:

    The 10 Most Scariest Things About Buy Macaw buy Macaw

  16. Please let me know if you’re looking for a article writer
    for your site. You have some really great articles and I think I would be a good asset.
    If you ever want to take some of the load off, I’d love to
    write some content for your blog in exchange for a
    link back to mine. Please shoot me an e-mail if interested.

    Kudos!

  17. CurtisBam表示:

    Indian pharmacy international shipping: india pharmacy mail order – Online medicine

  18. Pills prescribing information. Brand names.
    how to buy coumadin without a prescription
    Some about pills. Get here.

  19. It is the best time to make some plans for the future and it is time to be happy.
    I have read this post and if I could I wish to
    suggest you some interesting things or tips. Maybe you can write next articles referring to this
    article. I desire to read more things about it!

  20. CurtisBam表示:

    mexican drugstore online: buying prescription drugs in mexico – п»їbest mexican online pharmacies

  21. You actually make it seem so easy with your presentation but I find this topic to be really something which I think
    I would never understand. It seems too complicated and
    extremely broad for me. I’m looking forward for your next post, I’ll try
    to get the hang of it!

發佈留言

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