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,244,344 Responses

  1. вывод из запоя с выездом краснодар severussnape.borda.ru/?1-4-0-00000317-000-0-0-1730744751 .

  2. BernardMop表示:

    minocycline weight gain iver fast minocycline 50mg pills online

  3. BrandenSlomy表示:

    priligy maxpharm: buy dapoxetine online – cheap priligy

  4. вывод из запоя анонимно краснодар familyportal.forumrom.com/viewtopic.php?id=28570#p70369 .

  5. BrandenSlomy表示:

    buy priligy: buy priligy – priligy maxpharm

  6. Great beat ! I would like to apprentice while
    you amend your website, how could i subscribe for a weblog web site?
    The account aided me a appropriate deal. I were tiny
    bit familiar of this your broadcast offered brilliant transparent idea

  7. Nikefut表示:

    darknet market lists how to access dark web https://mydarknetmarketlinks.com/

  8. вывод из запоя анонимно краснодар http://belbeer.borda.ru/?1-6-0-00000757-000-0-0-1730745253 .

  9. нарколог вывод из запоя краснодар severussnape.borda.ru/?1-4-0-00000317-000-0-0-1730744751 .

  10. 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 same in support of you.

  11. вывод из запоя краснодар на дому dubna.myqip.ru/?1-5-0-00000283-000-0-0-17307453197 .

  12. DarrenUnjuh表示:

    https://plavixclo.com/# Plavix generic price

  13. nodeposit表示:

    Link exchange is nothing else but it is simply placing the other
    person’s web site link on your page at suitable place and
    other person will also do same in favor of you.

  14. срочная помощь вывод из запоя краснодар http://severussnape.borda.ru/?1-4-0-00000317-000-0-0-1730744751 .

  15. алкоголизм вывод из запоя краснодар алкоголизм вывод из запоя краснодар .

  16. вывод из запоя цены краснодар http://bija089.0pk.me/viewtopic.php?id=2554 .

  17. bwslot168表示:

    If you desire to grow your experience just keep visiting this web page and be updated with the newest
    information posted here.

  18. Value Your Insights!

    I’m Pleased you Encountered the Write-up Valuable.

    If you’re Curious about Uncovering more Avenues in the
    online Betting Realm, I’d Propose Trying out CMD368.

    They Provide a Vast assortment of Stimulating Bookmaking Avenues, Broadcasted events,
    and a User-friendly System.

    What I Especially Favor about CMD368 is their Emphasis to Prudent Sports.
    They have Stringent Security and Options to Facilitate Customers Remain in control.

    Irrespective if you’re a Adept Wagerer or Unfamiliar to the Betting, I Think you’d Really Love
    the Adventure.

    Feel free to Join Using the Link and Get in touch if you have Any other Curiosities.

    Stop by my web site: online sport betting

發佈留言

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