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,248,155 Responses

  1. Kevin Cook表示:

    Thanks for the useful post. More like this at Helpful site

  2. damansara heights house for sale

  3. BrandenSlomy表示:

    how to get clomid without insurance: clomid – where can i buy cheap clomid without prescription

  4. What’s up, just wanted to mention, I enjoyed this post. It was practical.
    Keep on posting!

  5. Stephenideox表示:

    Бесплатный информационный сервис, предоставляющий контент и справочную информацию о инвестировании.
    инвестиция денег

  6. Eric表示:

    A Intermediate Guide For Electric Fires For Media Wall media fireplace (Eric)

  7. See What Shop Glass Repair Tricks The Celebs Are Using shop glass repair (https://telegra.ph)

  8. AlbertDuM表示:

    purchase cytotec: cyt premium – Abortion pills online

  9. More interestingly, US players will find some of the highest-paying
    slot entertainment with an extensive section of Bitcoin progressive jackpot
    games.

  10. BernardMop表示:

    п»їplavix generic PlavixClo Best Price buy clopidogrel bisulfate

  11. BrandenSlomy表示:

    buying generic clomid without a prescription: clomid – clomid rx

  12. AlbertDuM表示:

    Cost of Plavix on Medicare: buy Plavix Clo – buy clopidogrel online

  13. AlbertDuM表示:

    plavix best price: Plavix Clo – Clopidogrel 75 MG price

  14. Wally Rima表示:

    buying land in florida

  15. AlbertDuM表示:

    Clopidogrel 75 MG price: PlavixClo Best Price – plavix medication

  16. BernardMop表示:

    lisinopril 20mg prices buy Lisinopril 1st buy Lisinopril online

  17. Somebody necessarily assist to make seriously articles I might state.
    This is the very first time I frequented your website page and
    to this point? I surprised with the research you made to create
    this particular put up incredible. Wonderful activity!

  18. Hattie Fuller表示:

    I enjoyed this read. For more, visit indian catering london

  19. DarrenUnjuh表示:

    https://lisinopril1st.com/# lisinopril1st

  20. You definitely made the point.

  21. AlbertDuM表示:

    prinivil 25mg: lisinopril mexico – lisinopril 10 mg price in india

  22. DerekWab表示:

    кухня на заказ – выбор для тех, кто хочет стильный и удобный интерьер.

  23. Stacy Rickett表示:

    agriculture land loan

  24. AlbertDuM表示:

    Lisinopril 1st: buy Lisinopril 1st – buy Lisinopril 1st

發佈留言

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