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,227,885 Responses

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

  2. Klaus表示:

    See What American Fridge Freezer With Water And Ice Dispenser
    Tricks The Celebs Are Using fridge freezer with water
    and ice (Klaus)

  3. 15 Presents For Those Who Are The Freestanding Fridge Freezer 50 50 Lover In Your Life 50/50 Fridge freezer

  4. ClydePab表示:

    matadorbet.bid: matadorbet.bid – matadorbet.bid

  5. You’ll Never Guess This Accident And Injury Attorneys’s Tricks accident and injury

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

  7. It is perfect time to make some plans for the longer term and it is time to be happy.
    I have learn this publish and if I could I wish to recommend you some fascinating things or tips.
    Perhaps you could write subsequent articles referring to this article.
    I wish to read more things about it!

  8. Тут можно преобрести сейф оружейный цена сейфы для оружия

  9. HassanbuifS表示:

    https://denemebonusuverensiteler.top/# denemebonusuverensiteler.top
    denemebonusuverensiteler.top

  10. LarryCoell表示:

    deneme bonusu veren siteler deneme bonusu veren siteler deneme bonusu veren siteler

  11. Derekfar表示:

    deneme bonusu veren siteler betturkey betturkey.com: deneme bonusu veren siteler yerliarama.org – deneme bonusu veren siteler

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

  13. Simply want to say your article is as astounding. The clearness in your post is just cool
    and i can assume you are an expert on this subject.
    Fine with your permission let me to grab your feed to keep updated with forthcoming
    post. Thanks a million and please carry on the rewarding work.

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

  15. France表示:

    What’s The Current Job Market For Treadmill Best Professionals?
    treadmill best (France)

  16. Fix表示:

    20 Up-Andcomers To Watch The Door Fitters Maidstone Industry Fix

  17. Hey There. I discovered your weblog the use of msn. That is a very well
    written article. I’ll make sure to bookmark it and come back to learn more
    of your useful information. Thank you for the post.
    I’ll certainly comeback.

    Feel free to visit my website … https://parentingliteracy.com/wiki/index.php/User:CJJGaye2281

  18. Your Family Will Be Grateful For Having This Vehicle Diagnostics Near Me Diagnostic Tests

  19. ClydePab表示:

    ultrabet tr online: ultrabet giris – ultrabet

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

  21. Carissa表示:

    20 Myths About Houston Asbestos Attorney: Busted mesothelioma attorneys (Carissa)

  22. Diplomi_sckn表示:

    купить старый диплом купить старый диплом .

  23. Тут можно преобрести купить сейф для охотничьего ружья в москве шкаф для оружия цена

  24. Derekfar表示:

    matadorbet bid: matadorbet.bid – matadorbet giris

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

  26. Тут можно преобрести купить сейф для охотничьего ружья в москве сейф для охотничьего ружья купить в москве

  27. RobertLax表示:

    This article has some fascinating insights—worth a look https://freeline.mn.co/posts/71608209

發佈留言

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