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,382 Responses

  1. Link exchange is nothing else however it is only placing the other person’s website link
    on your page at suitable place and other person will also do similar in favor of you.

  2. Webdesign表示:

    I was curious if you ever considered changing the structure of your
    website? 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 one or two images.
    Maybe you could space it out better?

  3. HenrydeT表示:

    pinup пин ап вход пин ап зеркало

  4. CurtisIncix表示:

    vavada: вавада онлайн казино – vavada-kazi.ru

  5. DarioTob表示:

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

  6. You actually suggested it very well!

  7. Jamesdiure表示:

    вавада онлайн казино: вавада – казино вавада

  8. CurtisIncix表示:

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

  9. Josephmax表示:

    https://pinup-kazi.ru/# пинап казино

  10. Excellent way of telling, and good paragraph to obtain information regarding my presentation subject matter,
    which i am going to deliver in academy.

  11. I want to to thank you for this excellent read!! I certainly enjoyed every bit of it.
    I have got you book marked to check out new stuff you post…

  12. The user has to choose the video quality by clicking in the lower right corner of the tool.

  13. Josephmax表示:

    http://pinup-kazi.ru/# пинап казино

  14. Hey there! Would you mind if I share your blog with my facebook group?
    There’s a lot of folks that I think would really enjoy your content.
    Please let me know. Thank you

  15. Iyannaobesk表示:

    актуальное 1win покер зеркало
    действующее 1win покер зеркало

  16. Jamesdiure表示:

    пин ап казино: pin up казино – pinup

  17. Josephmax表示:

    https://vavada-kazi.ru/# вавада онлайн казино

  18. Pretty component to content. I just stumbled upon your site and in accession capital
    to say that I acquire in fact loved account your weblog posts.
    Anyway I will be subscribing to your augment or even I achievement you access
    persistently fast.

  19. Good day! Would you mind if I share your blog with my zynga group?
    There’s a lot of people that I think would really appreciate your content.
    Please let senior living near me know.

    Many thanks

  20. DarioTob表示:

    pinup: pinup kazi – pinup-kazi.kz

  21. CurtisIncix表示:

    вавада казино онлайн: vavada – вавада казино онлайн

發佈留言

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