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,236,732 Responses

  1. Hi, i think that i saw you visited my website thus i came to “return the
    favor”.I am attempting to find things to improve
    my website!I suppose its ok to use some of your ideas!!

  2. 20 Trailblazers Lead The Way In Online Mystery Box
    Mystery box opening online

  3. See What Two Seater Fabric Sofa Uk Tricks The Celebs Are Using Two seater Fabric sofa

  4. I believe everything posted made a great deal of sense.
    However, think on this, suppose you were to create
    a killer headline? I am not suggesting your content is not solid,
    but suppose you added a title that makes people want more?
    I mean JS 依照元素出現數量排序 – 馬老師
    雲端研究室 is a little boring. You might look at Yahoo’s home page and watch how they create article titles to get viewers to click.
    You might add a related video or a related picture or
    two to grab people interested about everything’ve written.
    In my opinion, it could bring your website a little bit more interesting.

  5. An outstanding share! I have just forwarded this onto a coworker who has been conducting a little research on this.
    And he in fact bought me dinner because I found it for him…
    lol. So let me reword this…. Thanks for the meal!!
    But yeah, thanks for spending the time to talk about this subject
    here on your blog.

  6. Derekfar表示:

    slot tr online: slot oyunlar? – slot siteleri

  7. Fabulous, what a webpage it is! This webpage gives useful facts
    to us, keep it up.

  8. Hellen表示:

    This article is extremely valuable. I genuinely enjoyed the detailed explanation you shared.

    It’s obvious that you put a lot of research into producing this.
    Keep up the fantastic work. Thank you for putting this out there.
    I’ll surely come back to read more!

    My web-site Illinois Press – Hellen,

  9. LarryCoell表示:

    Casino Siteleri guvenilir casino siteleri Casino Siteleri

  10. Изучите игра ракета отзывы, чтобы узнать, как выигрывают опытные игроки.

  11. maryam表示:

    I have been surfing online more than three hours today, yet I never found
    any interesting article like yours. It is pretty worth enough for me.
    In my opinion, if all site owners and bloggers made good content as you did,
    the internet will be a lot more useful than ever before.

  12. The Reasons You Should Experience Upvc Window Repair At
    A Minimum, Once In Your Lifetime Double Glazed Window Repairs Near Me (Faucetrocket1.Bravejournal.Net)

  13. ClydePab表示:

    ultrabet tr online: ultrabet guncel – ultrabet yeni giris 1125

  14. Diplomi_yner表示:

    купить диплом курсовая работа prema365-diploms.ru .

  15. Chara表示:

    10 Things You’ve Learned In Kindergarden That’ll Help You With Sectional Sofa Bed Pull Out
    corner sofa pull out (Chara)

  16. What’s The Current Job Market For Pram And Pushchair 2
    In 1 Professionals? pram and pushchair 2 in 1

  17. Do You Know How To Explain Leather Chesterfield Sofa To Your Mom 2 seater leather chesterfield Sofa

  18. LarryCoell表示:

    guvenilir casino siteleri Casino Siteleri casino siteleri win

  19. Diplomi_coer表示:

    бланк диплома о высшем образовании ссср купить prema365-diploms.ru .

  20. ArnHesia表示:

    Searching for stromectol online brighter tomorrow pharmacy that offers a discount on its products?

  21. This Story Behind Online Mystery Box Can Haunt You Forever!
    Mystery Box Open Game (https://Digitaltibetan.Win/)

  22. Diplomi_jxer表示:

    купить аттестат отзывы купить аттестат отзывы .

  23. This post is priceless. When can I find out more?

    My web blog :: advert company

  24. Magnificent goods from you, man. I have remember your stuff previous
    to and you’re just too great. I actually
    like what you’ve bought here, certainly like what you’re stating and the way in which through which you assert it.

    You’re making it enjoyable and you continue to care for to keep it wise.
    I cant wait to learn much more from you. That is actually a wonderful website.

    my web page: advertisement platform

  25. Herototo表示:

    good for content , i give your apresiasion five star for you, good bro

發佈留言

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