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,480,639 Responses

  1. Click here表示:

    This is really attention-grabbing, You are a very
    skilled blogger. I have joined your feed and look forward to in the hunt
    for more of your excellent post. Additionally, I have shared your website in my social networks

  2. id=”firstHeading” class=”firstHeading mw-first-heading”>Search results

    Helр

    English

    Tools

    Tools
    mօvе to sidebar hide

    Actions

    Ԍeneral

    my web blog ค่าพวงหรีด กรมสรรพากร

  3. This blog was… how do I say it? Relevant!! Finally I’ve found something that helped me.
    Appreciate it!

  4. Spot on with this write-up, I actually believe that this amazing site needs a lot
    more attention. I’ll probably be back again to see more, thanks
    for the information!

  5. When someone writes an article he/she keeps the thought
    of a user in his/her mind that how a user can be aware of it.
    Therefore that’s why this post is perfect. Thanks!

    Also visit my page; Pinoy SEO Services Philippines

  6. Nikefut表示:

    dark web search engine dark market onion darkmarkets

  7. Ricardo Cook表示:

    Fantastic information about optimizing legal websites—definitely looking into hiring a reputable # marketing agency for lawyers #

  8. Hello! This post couldn’t be written any better!
    Reading through this post reminds me of my good old room mate!

    He always kept chatting about this. I will forward this page to him.
    Pretty sure he will have a good read. Many thanks for sharing!

  9. imp source表示:

    Good information. Lucky me I came across your site by accident (stumbleupon).
    I have bookmarked it for later!

  10. You really make it seem really easy together with your
    presentation but I to find this matter to
    be actually one thing that I feel I’d by no means understand.

    It kind of feels too complicated and very wide for me.
    I’m having a look ahead for your next submit, I will try to get the dangle
    of it!

  11. Godfather789表示:

    https://godfather-789.com/ยเว็บตรงของคนไทย เจ้าพ่อมาเฟียเว็บใหญ่ไม่มีโกง

發佈留言

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