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,431,550 Responses

  1. Diplomi_syoi表示:

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

  2. BrandonSaisy表示:

    top online pharmacy india: Indian pharmacy international shipping – top 10 online pharmacy in india

  3. Diplomi_ggoi表示:

    купить диплом в улан удэ prema-diploms.ru .

  4. Williamagric表示:

    male ed http://indianpharm24.pro/# buy prescription drugs from india

  5. Williamagric表示:

    ed treatment pills https://mexicanpharm24.cheap/# pharmacies in mexico that ship to usa

  6. The Secret Secrets Of Cut Car Key laser key Cutting

  7. LeroyBak表示:

    purple pharmacy mexico price list mexican drugs purple pharmacy mexico price list

  8. DevonPhack表示:

    http://mexicanpharm24.cheap/# pharmacies in mexico that ship to usa
    best natural cure for ed

  9. بت زنده表示:

    Hi, i think that i saw you visited my site thus i came to “return the favor”.I’m attempting to
    find things to enhance my web site!I suppose its ok to use a few of
    your ideas!!

  10. DevonPhack表示:

    https://indianpharm24.pro/# cheapest online pharmacy india
    how to get prescription drugs without doctor

  11. Williamagric表示:

    ed drug comparison http://mexicanpharm24.cheap/# mexican pharmaceuticals online

  12. Diplomi_gloi表示:

    купить диплом охраны prema-diploms.ru .

  13. DevonPhack表示:

    http://mexicanpharm24.cheap/# buying prescription drugs in mexico
    ed vacuum pumps

  14. fireplaces表示:

    9 Things Your Parents Taught You About Ethanol Fireplaces fireplaces

  15. Adolph表示:

    What’s The Current Job Market For Lightweight Folding Rollator Walker With Seat Professionals?
    lightweight folding rollator walker with seat, Adolph,

  16. Uganda tours表示:

    Hey! This post could not be written any better!

    Reading this post reminds me of my good old room mate! He always kept talking about
    this. I will forward this article to him. Pretty sure he will
    have a good read. Thanks for sharing!

  17. Medication information leaflet. Short-Term Effects.
    cost of generic proscar
    All trends of medicines. Read information now.

  18. Williamagric表示:

    non prescription ed drugs http://mexicanpharm24.cheap/# best online pharmacies in mexico

  19. Diplomi_kqoi表示:

    купить диплом тгу prema-diploms.ru .

  20. LeroyBak表示:

    buy medicines online in india Pharmacies in India that ship to USA india pharmacy mail order

  21. DevonPhack表示:

    http://indianpharm24.pro/# indian pharmacy
    viagra without a doctor prescription

  22. LeroyBak表示:

    mexican drugstore online Legit online Mexican pharmacy mexico drug stores pharmacies

  23. Markus表示:

    It Is The History Of Togel Sydney situs togel online (Markus)

  24. Hello! This is my first visit to your blog! We are
    a team of volunteers and starting a new initiative in a
    community in the same niche. Your blog provided us valuable information to work on. You have done a outstanding job!

  25. LeroyBak表示:

    ed treatment pills low cost pharmacy tadalafil without a doctor’s prescription

  26. I am regular reader, how are you everybody? This
    post posted at this site is truly fastidious.

    https://w6.datakorea.org/

  27. DevonPhack表示:

    http://mexicanpharm24.cheap/# best online pharmacies in mexico
    pharmacy drugs

發佈留言

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