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,250,079 Responses

  1. Why Everyone Is Talking About Fleshlight Sale Right Now fleshlight sex (289an1Ad92ak6p.com)

  2. fully furnished is not necessary an important factor

  3. I’m now not sure where you’re getting your information, but good topic.

    I needs to spend some time studying more or working out more.
    Thank you for fantastic information I was in search of this info for my mission.

  4. выведение из запоя воронеж стационар https://www.vip.mybb.rocks/viewtopic.php?id=7677 .

  5. Visit表示:

    I was curious if you ever thought of changing the layout of
    your blog? Its very well written; I love what youve got to
    say. But maybe you could a little more in the way
    of content so people could connect with it better.
    Youve got an awful lot of text for only having 1 or 2 images.
    Maybe you could space it out better?

  6. лечение наркозависимости в стационаре kyevlyn.ukrbb.net/viewtopic.php?f=2&t=13641 .

  7. вывод из запоя в стационаре воронежа https://advance2.ukrbb.net/viewtopic.php?f=2&t=780/ .

  8. вывод из запоя в стационаре воронежа http://www.ideya.forums.party/viewtopic.php?id=660 .

  9. лечение наркозависимости в стационаре лечение наркозависимости в стационаре .

  10. BrandenSlomy表示:

    buy priligy: dapoxetine price – buy dapoxetine online

  11. В интернет-магазине world-techcolor вы можете купить высококачественные лакокрасочные материалы для автомобиля

  12. выведение из запоя воронеж стационар vishivayu.ukrbb.net/viewtopic.php?f=12&t=13444 .

  13. выведение из запоя воронеж стационар http://www.dexanet.ukrbb.net/viewtopic.php?f=14&t=20393/ .

  14. I’m truly enjoying the design and layout of your site.
    It’s a very easy on the eyes which makes it much more pleasant for
    me to come here and visit more often. Did you hire out
    a developer to create your theme? Great work!

  15. BrandenSlomy表示:

    where to buy cheap clomid online: rexpharm – can you buy clomid

  16. Boyd Copus表示:

    car park another important point to consider

  17. BernardMop表示:

    generic plavix buy Plavix Clo Cost of Plavix on Medicare

  18. This is very fascinating, You are an excessively skilled blogger.
    I have joined your rss feed and look ahead to in the hunt for extra of your great
    post. Also, I’ve shared your web site in my social networks

    my webpage :: dumbbell upper body workout

  19. 5 Killer Queora Answers On Double Sided Dildos penetration toys double

  20. AlbertDuM表示:

    Cost of Plavix without insurance: antiplatelet drug – clopidogrel bisulfate 75 mg

發佈留言

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