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,479,213 Responses

  1. Undeniably imagine that that you said. Your favourite justification seemed to be at
    the internet the easiest thing to understand of. I say to you, I
    definitely get irked at the same time as other people consider issues that
    they just do not know about. You managed to hit the nail
    upon the top as neatly as outlined out the whole thing with no need side-effects ,
    other people could take a signal. Will probably be back to get more.
    Thank you

  2. HenrydeT表示:

    вавада казино зеркало вавада казино вавада казино онлайн

  3. Great looking site. Presume you did a whole lot of your own html coding.

    laziska.com.pl

  4. Thanks for sharing such a nice thinking, post is fastidious, thats why i have read it fully

  5. DarioTob表示:

    пин ап казино онлайн: pinup-kazi.kz – pinup kazi

  6. Someone necessarily assist to make significantly articles I would state.
    This is the first time I frequented your website page and thus far?
    I amazed with the research you made to create this actual
    put up incredible. Magnificent process!

    Here is my homepage – box set movies

  7. CurtisIncix表示:

    vavada: vavada kazi – вавада казино

  8. DarioTob表示:

    вавада казино зеркало: vavada kazi – vavada

  9. Josephmax表示:

    https://pinup-kazi.ru/# pinup kazi

  10. qzfCzS.Com表示:

    Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something.

    I think that you can do with some pics to drive the message home a
    bit, but instead of that, this is wonderful blog.
    A fantastic read. I’ll definitely be back. http://Qzfczs.com/comment/html/?525101.html

  11. Jamesdiure表示:

    пинап казино: pinup – пин ап казино официальный сайт

  12. xxx表示:

    Excellent article. Keep posting such kind of info on your page.
    Im really impressed by your blog.
    Hi there, You’ve done an excellent job. I’ll definitely digg it and individually recommend to my friends.
    I’m confident they’ll be benefited from this website.

  13. Sweet blog! I found it while surfing around on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!

    Many thanks

  14. CurtisIncix表示:

    pinup-kazi.ru: пин ап казино – пин ап вход

  15. выведение из запоя врачом наркологом http://www.ekonomimvmeste.ukrbb.net/viewtopic.php?f=14&t=65376/ .

  16. Jamesdiure表示:

    пин ап казино: пинап казино – пин ап вход

  17. You’ve inspired me to reconsider my patio area entirely with those leading edge techniques round hardscaping—can’t wait to get begun!! excavation companies Concord

  18. John Allison表示:

    Gran guía sobre cómo evitar problemas legales Página de inicio

  19. You are so awesome! I do not think I’ve read through
    anything like this before. So nice to find someone with genuine thoughts on this subject matter.
    Seriously.. many thanks for starting this up. This website is one thing that is required on the
    web, someone with a bit of originality!

    my web page nsk dental handpiece

  20. HenrydeT表示:

    пин ап кз pinup пинап казино

  21. I believe that is one of the so much important info for
    me. And i am happy studying your article. But should statement on some common things, The website style is ideal, the articles is in reality great : D.
    Just right activity, cheers

發佈留言

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