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,266,015 Responses

  1. Way cool! Some extremely valid points! I appreciate you writing this
    post and the rest of the website is extremely good.

  2. goldpay表示:

    Casino Slot Machines goldpay

  3. Hi, just wanted to tell you, I loved this blog post. It was helpful.
    Keep on posting!

  4. Terrific tips, Cheers.

  5. Hello There. I found your blog using msn. This is a very well written article.
    I’ll be sure to bookmark it and come back to read more of your useful information. Thanks
    for the post. I will certainly return.

    Feel free to visit my website; best realtor in st johns county fl

  6. Leila Klein表示:

    Nunca había considerado cómo las leyes pueden afectar el valor de la propiedad a largo plazo; gracias por abrirme los ojos a eso! Fuente del artículo

  7. DarioTob表示:

    пин ап зеркало: пин ап казино официальный сайт – pinup

  8. Ann Parsons表示:

    Appreciate the thorough analysis. For more, visit Bathroom remodeling

  9. JacobLoody表示:

    кухни на заказ от производителя – Профессиональное изготовление кухонь на заказ с гарантией качества.

  10. useful site表示:

    It’s hard to come by experienced people for this topic, but you sound like you know
    what you’re talking about! Thanks

  11. DarioTob表示:

    пин ап казино онлайн: pinup-kazi.kz – pin up казино

  12. Top Money Making Apps in Pakistan, Popular Money Earning Apps in Pakistan
    money earning apps in pakistan online earning apps in pakistan .

  13. PG SOFT Gaming – best slot online in the Philippines
    better than jili slot and bingo plus
    free Spins 3000+ Free Demo Slot

    searching pggaming.org on Google or Bing to get free coins for play

    https://pggaming.org/

  14. Howdy I am so delighted I found your blog, I really found you by mistake, while
    I was researching on Yahoo for something else, Regardless I
    am here now and would just like to say thanks for
    a tremendous post and a all round interesting blog (I also love the theme/design),
    I don’t have time to read it all at the moment but I have bookmarked
    it and also added your RSS feeds, so when I have time I will be back to read a great deal more,
    Please do keep up the great b.

  15. find more表示:

    I’ve been surfing online more than 4 hours today, yet I
    never found any interesting article like yours. It’s pretty
    worth enough for me. In my view, if all webmasters and bloggers made good content as
    you did, the web will be much more useful than ever before.

  16. DarioTob表示:

    пин ап казино официальный сайт: пин ап казино – пин ап казино

  17. Asking questions are genuinely nice thing if you are not understanding anything completely,
    except this post offers nice understanding even.

    Take a look at my web-site … best realtor in volusia county fl

  18. DarioTob表示:

    пин ап кз: пинап казино – pinup-kazi.kz

  19. HenrydeT表示:

    вавада казино зеркало vavada kazi vavada-kazi.ru

  20. DarioTob表示:

    пин ап кз: пин ап кз – pinup-kazi.kz

  21. HenrydeT表示:

    пин ап казино pinup kazi pin up казино

  22. DarioTob表示:

    пин ап зеркало: пин ап казино официальный сайт – pinup

  23. Jamesdiure表示:

    вавада казино зеркало: вавада онлайн казино – vavada

  24. Hello to all, how is the whole thing, I think every one is getting more from this web page,
    and your views are nice for new users.

    My web blog; pronerve 6 ingredients

發佈留言

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