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,365,042 Responses

  1. Tillie Clark表示:

    Your tips on spotting roof damage are spot on! Many people don’t realize how easy it is to miss issues until it’s too late tile repair

  2. Sue Castro表示:

    It’s interesting to learn about historical roofing styles shingle roofers

  3. I’m planning to install solar panels residential roofing

  4. Timothy Blake表示:

    The discussion around client-centric approaches in web design was insightful—get practical design tips via seo company for legal firms

  5. Henry Lyons表示:

    If you’re looking for something eco-friendly, metal roofs are recyclable! Find out more benefits at roofers vancouver

  6. Loved how you addressed balancing workloads amongst attorneys while ensuring each member feels valued contributing overall success towards growth initiatives moving forward through seo companies for lawyers

  7. Wonderful tips on how to write effective blog posts as a lawyer! Content is king in legal SEO too! For additional advice, check out seo for legal firm

  8. Iyannaobesk表示:

    jack poker реальные отзывы
    jack poker отзывы игроков

  9. So glad I stumbled upon this blog post—it has given me many ideas regarding what kind of roof would suit my home best! metal roofers

  10. Wow, this is eye-opening! I need to find a seo company for lawyers to take my firm’s online marketing to the next level

  11. Teresa Martin表示:

    The versatility in design with modern metal roofing is remarkable—it fits so many architectural styles! Discover designs at roofing contractors vancouver

  12. I do accept as true with all the ideas you have presented in your post.

    They are really convincing and can certainly work.
    Nonetheless, the posts are very short for starters. May you please
    extend them a bit from next time? Thank you for the post.

  13. Dereketerm表示:

    buy amoxicillin online no prescription: cheap amoxil – amoxicillin 200 mg tablet

  14. Way cool! Some very valid points! I appreciate you penning this article
    and the rest of the website is really good.

    Here is my homepage :: Pinoy SEO Services

  15. Great job clarifying methods behind improving website load speed—a critical aspect affecting user experience directly impacting retention rates as discussed through seo for legal firm

  16. scam表示:

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now
    each time a comment is added I get three emails with
    the same comment. Is there any way you can remove people from that service?
    Thanks!

  17. Nikefut表示:

    darknet market dark market link https://darknetmarketstore.com/ – dark web link

  18. Dereketerm表示:

    amoxicillin order online no prescription: amoxicillin 500mg capsules price – how to buy amoxicillin online

  19. You are so interesting! I don’t think I’ve truly read through a single thing
    like that before. So wonderful to discover somebody with a few
    unique thoughts on this issue. Really.. thank you for starting this up.
    This website is something that’s needed on the internet,
    someone with a bit of originality!

    Here is my web blog; Copper peptide serum

  20. Andboo表示:

    Good day I am so happy I found your blog, I really found you
    by mistake, while I was browsing on Aol for something
    else, Anyhow I am here now and would just like to say cheers for
    a fantastic post and a all round interesting blog (I also love the
    theme/design), I don’t have time to go through it
    all at the moment but I have bookmarked it and also included your RSS feeds, so
    when I have time I will be back to read more, Please
    do keep up the superb job.

  21. When someone writes an post he/she keeps the thought of a user in his/her brain that how a
    user can be aware of it. So that’s why this post is outstdanding.
    Thanks!

  22. Hello fantastic blog! Does running a blog similar to this
    take a lot of work? I’ve no knowledge of programming but I was hoping to start my
    own blog in the near future. Anyways, should you have any recommendations or techniques for new
    blog owners please share. I know this is off subject however I just
    wanted to ask. Many thanks!

    Check out my webpage personal injury lawyer phoenix az

  23. Dereketerm表示:

    amoxicillin 500 mg for sale: com pharm – amoxicillin 200 mg tablet

發佈留言

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