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,669,314 Responses

  1. See What Cost For Replacement Car Key Tricks The Celebs Are Utilizing cost For replacement car key
    (http://125.141.133.9:7001/bbs/board.php?bo_table=free&wr_id=2116145)

  2. Williamagric表示:

    erection pills that work http://mexicanpharm24.cheap/# mexican border pharmacies shipping to usa

  3. Williamagric表示:

    the canadian drugstore https://mexicanpharm24.cheap/# medication from mexico pharmacy

  4. Shaneevige表示:

    Just desire to say your article is as astonishing. The clarity in your post is simply excellent and i can assume you’re an expert on this subject. Well with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please keep up the rewarding work.

    xxx

  5. вывод из запоя цены на дому королев вывод из запоя цены на дому королев .

  6. капельница от запоя на дому подольск капельница от запоя на дому подольск .

  7. 20 Trailblazers Setting The Standard In Audi Key Replacement
    audi Replacement Keys

  8. OGAPAR表示:

    Usually I do not learn post on blogs, but I would like to say that this write-up very forced me
    to check out and do so! Your writing taste has been surprised me.
    Thank you, quite great post.

  9. Jorgeagopy表示:

    medication from mexico pharmacy: Mexican pharmacy ship US – mexican mail order pharmacies

  10. Jorgeagopy表示:

    over the counter ed treatment: cheap medication – best price for generic viagra on the internet

  11. Private ADHD Assessment Edinburgh Tools To Help You Manage Your Everyday Lifethe
    Only Private ADHD Assessment Edinburgh Trick That
    Everybody Should Know Private Adhd assessment edinburgh

  12. The 9 Things Your Parents Teach You About 2 Seater Fabric Sofa Sale 2 seater fabric Sofa sale

  13. вывод из запоя круглосуточно королев вывод из запоя круглосуточно королев .

  14. Williamagric表示:

    psychological ed treatment https://mexicanpharm24.cheap/# buying from online mexican pharmacy

  15. Simply want to say your article is as astounding. The clarity for your
    put up is just great and that i can think you’re an expert on this subject.
    Fine along with your permission let me to grasp your
    RSS feed to stay up to date with approaching post.
    Thanks one million and please keep up the enjoyable work.

  16. вывод из запоя королев наркология вывод из запоя королев наркология .

  17. 10 Inspiring Images About Situs Terpercaya Situs Togel Terpercaya (Situsterpercaya73623.Life3Dblog.Com)

  18. сколько стоит капельница на дому от запоя сколько стоит капельница на дому от запоя .

  19. вывод из запоя королев наркология вывод из запоя королев наркология .

  20. Ϝor latest news you haᴠe tⲟ pay a quick visit web and
    ᧐n internet I folund this site as a most excellent site for lateѕt updates.

    mʏ web site :: wismabet slot login

發佈回覆給「vivod iz zapoya korolev_kxEt」的留言 取消回覆

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