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,432,753 Responses

  1. Jorgeagopy表示:

    ed in young men: low cost pharmacy – best ed pills

  2. LeroyBak表示:

    ed medication online cheap medication discount prescription drugs

  3. bolatangkas表示:

    I’m very pleased to uncover this page. I wanted to thank you
    for ones time for this wonderful read!! I definitely savored every little bit of it and i
    also have you saved as a favorite to look at new stuff on your website.

  4. Williamagric表示:

    best ed pills that work http://mexicanpharm24.cheap/# reputable mexican pharmacies online

  5. \\\mega表示:

    Hello, its pleasant article about media print,
    we all understand media is a impressive source
    of information.

  6. BrandonSaisy表示:

    п»їbest mexican online pharmacies: mexican pharmacy – mexican mail order pharmacies

  7. What’s The Current Job Market For Corner Sofa Fabric Professionals?

    Corner sofa Fabric

  8. LeroyBak表示:

    erectile dysfunction pills low cost pharmacy legal to buy prescription drugs from canada

  9. The Top Reasons Why People Succeed With The Reprogramming Car Key Industry reprogram Key fob

  10. 20 Fun Infographics About Gray Fabric Corner Sofa
    Fabric Corner Sofa With Recliner

  11. Еще одной фишкой от 1xBet является Тото «Кибер
    футбол», где участникам предлагается угадать результаты 15-ти матчей FIFA.

  12. Guide To Double Glazing Near Me: The Intermediate Guide In Double Glazing
    Near Me double glazing near Me

  13. What’s The Job Market For Maxi Cosi Pebble Plus
    Car Seat Professionals? maxi cosi Pebble plus car Seat

  14. выведение из запоя балашиха стационар https://vyvod-iz-zapoya-v-stacionare-balashiha13.ru .

  15. выведение из запоя балашиха стационар https://vyvod-iz-zapoya-v-stacionare-balashiha11.ru .

  16. BrandonSaisy表示:

    п»їlegitimate online pharmacies india: Indian pharmacy international shipping – online shopping pharmacy india

  17. Five Killer Quora Answers On L Shaped Couch Sectional l shaped couch sectional

  18. Williamagric表示:

    pump for ed https://pharm24.pro/# how to overcome ed naturally

  19. This blog is the goat! Nothing but net!

  20. Ten Upvc Doors That Really Improve Your Life Replacement Upvc Door handle

發佈留言

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