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,236,998 Responses

  1. WilliamRhype表示:

    https://cytotec.club/# cytotec online

  2. Who Is The World’s Top Expert On Online Shop Designer Suits?

    low speed high torque Motor

  3. Ronaldpsymn表示:

    http://nolvadex.life/# is nolvadex legal

  4. Davidtes表示:

    cost of propecia for sale order cheap propecia prices cost of propecia tablets

  5. What’s The Current Job Market For Tier 2 Seo Professionals?
    tier 2 Backlinks

  6. Watch Out: How Central Locking Repairs Near Me Is Taking Over And What We Can Do About It car Lock replacement

  7. See What Replacement Sash Windows Tricks The Celebs Are Using replacement sash windows

  8. WilliamRhype表示:

    https://finasteride.store/# order generic propecia without insurance

  9. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 10 mg tablet

  10. RandallTaulp表示:

    lisinopril 10 mg canada: cost of lisinopril 40 mg – lisinopril 40 mg india

  11. 15 Up-And-Coming Work From Anywhere In The World Online Bloggers You Need To Check Out Part time Work from home

  12. 5 Double Glazing Offers Near Me Projects For Any Budget double Glazing replacement Window
    (ezproxy.cityu.Edu.hk)

  13. Movies表示:

    I think this is among the most significant information for me.
    And i’m glad reading your article. But wanna remark on few general things, The site style is ideal,
    the articles is really excellent : D. Good job, cheers

  14. Thanks for sharing your thoughts about Akcyza usa.
    Regards

  15. Five Killer Quora Answers On Locksmith Car Keys
    locksmith car keys

  16. You’ll Never Be Able To Figure Out This Window Replacement Near Me’s Tricks Window Replacement Near Me

  17. The Secret Secrets Of Boat Accident Lawyers boat Accident lawsuits

  18. I loved as much as you will receive carried out right here.

    The sketch is attractive, your authored material
    stylish. nonetheless, you command get got an shakiness over that you
    wish be delivering the following. unwell unquestionably
    come more formerly again as exactly the same nearly very often inside
    case you shield this hike.

  19. nirwana88表示:

    Quaⅼіty content is the secrеt to attract the viewers to pay a qhicқ visit
    the web page, that’s what thіs web site is
    providing.

    my web blog – nirwana88

  20. mini key Fob表示:

    9 Things Your Parents Teach You About Mini Key Fobs mini key Fob

  21. Davidtes表示:

    buy generic ciprofloxacin buy cipro cheap cipro ciprofloxacin

  22. Olive表示:

    I absolutely love your blog.. Excellent colors & theme.
    Did you make this site yourself? Please reply back as I’m wanting to create my own website and
    would like to learn where you got this from or exactly what the theme is
    named. Many thanks!

  23. Davidtes表示:

    ciprofloxacin over the counter buy cipro online canada buy cipro online

  24. How To Become A Prosperous Accident Lawyer Even If You’re Not Business-Savvy accident Lawsuit

  25. nearest表示:

    The People Nearest To Ecommerce SEO Agency Tell You Some Big Secrets nearest

  26. Annett表示:

    15 Door Repairs Near Me Benefits That Everyone Should Be Able To upvc door repairs near me
    [Annett]

發佈留言

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