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

  1. DavidBaide表示:

    purple pharmacy mexico price list http://mexicanpharmgate.com/ buying prescription drugs in mexico

  2. DarrenUnjuh表示:

    https://iverfast.com/# cost of ivermectin 3mg tablets

  3. Top Money Making Apps in Pakistan, For Extra Income
    top 10 earning apps in pakistan earn money app in pakistan .

  4. slot gacor表示:

    Hello, constantly i used to check web site posts here in the early hours in the morning, for the reason that i love to learn more and more.

  5. DavidBaide表示:

    buying prescription drugs in mexico https://mexicanpharmgate.com/ medication from mexico pharmacy

  6. Top Money Making Apps in Pakistan, Money Earning Apps in Pakistan: Features and Benefits
    pakistan online earning apps pakistan earning app .

  7. scam表示:

    Excellent beat ! I wish to apprentice at the same time as you amend your
    website, how can i subscribe for a weblog web site? The account helped me a acceptable deal.
    I had been tiny bit acquainted of this your broadcast offered vibrant clear
    idea

  8. feet fetish表示:

    It’s great that you are getting ideas from this paragraph as well as from our dialogue made
    here.

  9. Saved as a favorite, I love your blog!

  10. Best Money Making Apps in Pakistan, Money Earning Apps in Pakistan: How to Choose the Best
    earning app pakistan easy earning app in pakistan .

  11. This website was… how do you say it? Relevant!!

    Finally I’ve found something that helped me.

    Thank you!

  12. BrandenSlomy表示:

    cheap clomid: generic clomid – can i buy cheap clomid price

  13. TimothyNib表示:

    Удерживает известные соц силье: Facebook, VK, Instagram, Одноклассники равновеликим манером так далее Чечен Замещает тг выберет точно по экспресс-фото от мала до велика инфу что касается народе!
    глаз бога телеграмм бесплатно Два хунта а также Глаз Бога приметит все данные относительный авто. Ядро на десятки мнение изо распахнутых источников.

  14. Hi! I’ve been reading your blog for a while now and finally got the courage to go ahead and give you
    a shout out from Houston Texas! Just wanted to tell you
    keep up the excellent work!

發佈留言

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