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...

806,951 Responses

  1. %%

    Here is my site … leather trench Coat mens (yourspaceaway.com)

  2. %%

    Feel free to surf to my homepage … sexdoll for men (http://bulangiul.net/sexdollss611711)

  3. What i don’t understood is in truth how you’re now not actually
    a lot more smartly-appreciated than you may
    be now. You are so intelligent. You already know therefore significantly in terms of this subject, produced me personally
    imagine it from a lot of numerous angles. Its like women and men aren’t fascinated except it’s one thing to do with Girl gaga!
    Your own stuffs great. At all times handle it up!

  4. Guess the exchange rate, bitcoin and get money. Start with
    $10 and you can earn up to $1000 in a day, see how Here

  5. Penpek.Com表示:

    %%

    Here is my web blog … Double Glazing In Enfield (Penpek.Com)

  6. Visit here表示:

    Hola! I’ve been reading your blog for a while now
    and finally got the bravery to go ahead and give you a shout
    out from Austin Texas! Just wanted to say keep up
    the fantastic work!

  7. Jameslow表示:

    darknet dream market reddit Abacus Market darknet

  8. I’m extremely impressed with your writing skills and also with
    the layout on your weblog. Is this a paid theme or did you modify it
    yourself? Anyway keep up the nice quality writing, it’s rare
    to see a great blog like this one Fit Today Wellness Keto.

  9. Sexdolls Sale表示:

    %%

    Visit my webpage … Sexdolls Sale

  10. I read this article fully on the topic of the comparison of most recent and preceding technologies,
    it’s amazing article.

  11. MichaelMuh表示:

    мега официальный сайт магазины даркнета

  12. web表示:

    What’s up it’s me, I am also visiting this web site regularly,
    this site is really pleasant and the visitors are in fact
    sharing fastidious thoughts.

  13. THC gummies paris
    gummies are just one of the most preferred THC products.

  14. Corrysig表示:

    darknet market reddit darknet adress

  15. Calvindit表示:

    canadianpharmacyworld canadian online drugstore

  16. Brandendooky表示:

    ссылки на даркнет mega онион сайт

  17. This is very interesting, You’re a very skilled blogger. I have joined
    your rss feed and look forward to seeking more of your magnificent post.
    Also, I’ve shared your web site in my social networks!

  18. Sexdoll Sales表示:

    %%

    Here is my website Sexdoll Sales

發佈留言

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