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,374,066 Responses

  1. id=”firstHeading” class=”firstHeading mw-first-heading”>Search гesults

    Hеlp

    Engliush

    Toos

    Tools
    m᧐ve tо sidebar hide

    Actions

    Geneгɑl

    Нere iѕ my webpage :: ส่งดอกไม้เซอร์ไพรส์คนพิเศษ

  2. Markdob表示:

    Всем привет! Может кто знает, где почитатьразные статьи о недвижимости? Сейчас читаю – https://kovka09.ru

  3. LeonardKnism表示:

    https://tomason-russia.ru/ – Высокое качество и стиль в каждой детали вашей кухни.

  4. A motivating discussion is worth comment. I do believe
    that you ought to write more on this issue, it might not be a
    taboo subject but generally people do not speak about such subjects.
    To the next! All the best!!

  5. Узнай все о гистероскопия матки удаление полипа москваоперация по удалению полипа эндометрия в матке

  6. Markdob表示:

    Приветствую. Может кто знает, где почитатьразные статьи о недвижимости? Пока нашел – https://kovka09.ru

  7. DarrenUnjuh表示:

    https://plavixclo.com/# Plavix 75 mg price

  8. DavidBaide表示:

    buying prescription drugs in mexico http://mexicanpharmgate.com/ mexico pharmacies prescription drugs

  9. Markdob表示:

    Всем привет! Может кто знает, где найтиразные статьи о недвижимости? Пока нашел – https://kovka09.ru

  10. This was highly useful. For more, visit mobile tire service

  11. Markdob表示:

    Приветствую. Подскажите, где найтиполезные статьи о недвижимости? Сейчас читаю – https://kovka09.ru

  12. Eugene Scott表示:

    Seeing the improvement from a pressure wash is absolutely pleasing; visit pressure washing if you want those results as

  13. DavidBaide表示:

    best online pharmacies in mexico https://mexicanpharmgate.com/ buying prescription drugs in mexico

  14. Nikefut表示:

    free dark web how to get on dark web https://darknetmarketstore.com/ – deep web links

  15. DavidBaide表示:

    reputable mexican pharmacies online https://mexicanpharmgate.com/ buying prescription drugs in mexico online

  16. Horacio表示:

    Hi, Neat post. There’s an issue with your website in internet explorer, would test this?
    IE still is the marketplace leader and a huge component of other people will miss your
    wonderful writing due to this problem.

  17. Asking questions are in fact fastidious thing if you are not understanding anything entirely, however this paragraph
    gives pleasant understanding even.

  18. Узнай все о удаление полипов в матке в москвеудаление полипа эндометрии

  19. wps表示:

    fantastic points altogether, you simply received a
    new reader. What may you recommend in regards to your put up that you just made a few days in the past?
    Any sure?

  20. This information is priceless. Where can I find out more?

發佈留言

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