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,684,488 Responses

  1. Jackpot bet表示:

    Ꮃhat’s up to аll, for the reason that Ӏ am in fаct eager ᧐f
    reading this blog’ѕ post tο be updated on ɑ
    regular basis. It consists оf fastidious data.

    Aⅼso visit my web site … Jackpot bet

  2. Jariornii表示:

    Наши специалисты предлагают выгодно заказать диплом, который выполняется на бланке ГОЗНАКа и заверен печатями, водяными знаками, подписями. Данный документ пройдет лубую проверку, даже с применением специфических приборов. androidinweb.ru/ofitsialnyie-diplomyi-kupit-po-vashemu-zaprosu

  3. Williamwhogs表示:

    olympe casino: olympe – olympe casino

  4. Saved as a favorite, I like your web site!

  5. Ronaldbet表示:

    olympe casino casino olympe

  6. VictorVal表示:

    olympe casino: casino olympe – casino olympe

  7. AlbertShund表示:

    https://olympecasino.pro/# olympe casino

  8. www.ncch.ir表示:

    Cabinetry contractors focus on details and space-planning skills can help you squeeze
    functionality from even the tiniest corners of your
    Redwood City, CA home.

  9. mostbet_lsPr表示:

    мостбет войти https://mostbet5009.ru .

  10. VictorVal表示:

    olympe casino en ligne: casino olympe – olympe casino

  11. I was recommended this website by my cousin. I’m not sure whether this
    post is written by him as nobody else know such detailed about my difficulty.
    You’re incredible! Thanks!

  12. I relish, lead to I discovered just what I was looking for.
    You have ended my 4 day lengthy hunt! God Bless you man. Have a
    great day. Bye

  13. Gregoryron表示:

    olympe casino avis: casino olympe – olympe casino en ligne

  14. Williamwhogs表示:

    olympe casino: olympe casino cresus – olympe casino

  15. Tubidy表示:

    I have to thank you for the efforts you have put in penning this website.

  16. Intriguing material, I’ll be back to inspect a
    lot more.

  17. AlbertShund表示:

    https://olympecasino.pro/# olympe casino cresus

  18. Williamwhogs表示:

    olympe casino cresus: olympe casino en ligne – olympe casino cresus

  19. sell your Van表示:

    magnificent post, very informative. I wonder why the other experts of
    this sector do not understand this. You must continue your writing.
    I am confident, you’ve a huge readers’ base already!

  20. Hi everyone, it’s my first visit at this site,
    and article is genuinely fruitful in favor of me, keep up posting these posts.

  21. desi bf sex表示:

    Good tale, however it is not extremely clear regarding the desi bf sex experience.

發佈回覆給「soglasovanie pereplanirovki kvartiri_rjor」的留言 取消回覆

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