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,247,958 Responses

  1. BernardMop表示:

    buy cytotec cheapest cytotec Cytotec 200mcg price

  2. Thank you for the good writeup. It in fact was a amusement
    account it. Look advanced to far added agreeable from you!
    By the way, how can we communicate?

  3. Michael Memos表示:

    klcc land owner

  4. slot gacor表示:

    you’re in point of fact a good webmaster. The
    site loading speed is incredible. It kind of feels that you’re doing any distinctive trick.
    In addition, The contents are masterpiece. you have
    done a wonderful process in this topic!

  5. webpage表示:

    All transactions are kept in a public ledger, making it virtually unhackable.

  6. Your Worst Nightmare About L Shaped Bunk Beds For Kids
    Be Realized full over full l shaped bunk beds [Baseleg05.werite.net]

  7. Earl Kirtley表示:

    not all land can be bought

  8. Kolyaska_enPl表示:

    Bugaboo: комфорт и безопасность в каждой детали, Купить коляску Bugaboo – значит сделать правильный выбор, Как выбрать идеальную коляску Bugaboo для вашего малыша, Bugaboo: стиль и функциональность в одном, Bugaboo: легкость и управляемость в каждом краше, Какая коляска Bugaboo подойдет вашей семье, Bugaboo Bee: стиль и удобство для малыша, Bugaboo Buffalo: коляска для любых приключений.
    bugaboo ant коляска https://kolyaski-bugaboo-msk.ru/ .

  9. Jefferson表示:

    Heya i’m for the first time here. I came across this board and I find
    It truly useful & it helped me out much. I hope to give something back and aid
    others like you helped me.

  10. Triawngle Billiards & Bar Stools
    1471 Nisson Rd, Tustin,
    ϹA 92780, United States
    +17147715380
    Triangle Billiards Repair Kit

  11. excellent submit, very informative. I ponder why the other specialists of this sector don’t notice this.
    You must continue your writing. I’m sure, you have a great readers’ base already!

  12. BernardMop表示:

    cytotec pills buy online buy cytotec online Abortion pills online

  13. Spin189表示:

    When I originally commented I appear to have clicked the -Notify me when new comments are added-
    checkbox and from now on every time a comment is added I get four emails with the exact same comment.
    Is there an easy method you are able to remove me from that service?
    Cheers!

  14. BrandenSlomy表示:

    amoxicillin 50 mg tablets: cheap amoxil – where to buy amoxicillin

  15. AlbertDuM表示:

    generic plavix: Clopidogrel Best Prices – plavix medication

  16. Sauce bar表示:

    Every weekend i used to pay a visit this web site, as i wish for enjoyment, for the reason that this this
    website conations really good funny data too.

發佈留言

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