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,372,280 Responses

  1. BrandenSlomy表示:

    prednisone 2.5 mg cost: order Prednisone – prednisolone prednisone

  2. электрокарнизы для штор электрокарнизы для штор .

  3. StevenNub表示:

    priligy max pharm: dapoxetine price – cheap priligy

  4. Medicament information leaflet. Drug Class.
    get cheap sumatriptan without insurance
    All trends of drugs. Get information here.

  5. Markdob表示:

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

  6. I pay a quick visit day-to-day a few web pages and information sites to read articles or reviews,
    except this website offers quality based content.

  7. механизм для штор электрический elektrokarniz495.ru .

  8. BrandenSlomy表示:

    get generic clomid without dr prescription: clomid rex pharm – can i get clomid no prescription

  9. Dereketerm表示:

    amoxicillin 500 mg: Com Pharm – amoxicillin from canada

  10. What’s up Dear, are you actually visiting this site daily, if so then you will definitely get good experience.

  11. Markdob表示:

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

  12. Nikefut表示:

    tor marketplace free dark web https://darknetmarketstore.com/ – tor markets

  13. StevenNub表示:

    amoxicillin 500 tablet: Amoxicillin for sale – buy amoxicillin 500mg

  14. Markdob表示:

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

  15. slot gacor表示:

    I’m really inspired together with your writing abilities
    as well as with the format in your weblog. Is that this a paid subject or
    did you customize it yourself? Either way keep up the excellent quality writing, it’s rare to see a nice weblog like this one nowadays..

  16. StevenNub表示:

    amoxicillin 825 mg: amoxil com pharm – antibiotic amoxicillin

  17. StevenNub表示:

    priligy maxpharm: buy dapoxetine online – priligy maxpharm

  18. 20113995A表示:

    I am regular visitor, how are you everybody? This paragraph posted at this site is genuinely pleasant.

  19. If you wish for to take much from this post then you have to
    apply such methods to your won web site.

    Here is my website – http://Zian100PI.Com/discuz/home.php?mod=space&uid=1055806&do=profile&from=space

  20. BrandenSlomy表示:

    where can i buy cheap clomid online: generic clomid – cost of cheap clomid

發佈留言

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