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,374,452 Responses

  1. Mabel表示:

    What Is Robot Vacuum And Why Is Everyone Dissing It?
    robot vacuum price (Mabel)

  2. Guide To Vauxhall Adam Key Cover: The Intermediate Guide The Steps To
    Vauxhall Adam Key Cover vauxhall adam key cover

  3. sex pha trinh表示:

    This post is worth everyone’s attention. How can I find out more?

  4. Cole Dean表示:

    If you need garage door repairs, make sure to contact a trusted Garage door installation near me in Brandon, FL

  5. yd.Yichang.cc表示:

    Five All Terrain 3 Wheel Stroller Lessons From The Pros 3 Wheel Push chair, yd.Yichang.cc,

  6. Located on the southwestern corner of the island of Oʻahu, Kapolei is a modern hub for Hawaiians looking for urban amenities without the level of busyness the state’s capital, Honolulu, can attract.

    Feel free to surf to my web blog – https://fxflare.kr/%EC%9C%A0%ED%9D%A5%EC%95%8C%EB%B0%94-%ED%95%98%EB%8A%94%EB%B2%95-%EC%97%AC%EC%84%B1-%EA%B5%AC%EC%9D%B8%EA%B5%AC%EC%A7%81-%EA%B0%80%EC%9D%B4%EB%93%9C/

  7. Very nice post. I just stumbled upon your blog and wished
    to say that I’ve truly enjoyed browsing your blog posts.
    In any case I will be subscribing to your feed and I
    hope you write again soon!

  8. This was a great help. Check out driveway paving near me for more

  9. Hey there! I understand this is somewhat off-topic however I
    had to ask. Does operating a well-established website such as yours take a massive amount work?

    I’m completely new to operating a blog but I do write in my
    journal daily. I’d like to start a blog so I can easily
    share my own experience and feelings online. Please let me know if
    you have any kind of ideas or tips for new aspiring blog
    owners. Thankyou!

  10. This is a very good tip especially to those new to
    the blogosphere. Short but very accurate information… Appreciate your sharing this one.
    A must read article!

  11. снять ломку [url=www.ya.7bb.ru/viewtopic.php?id=14633]снять ломку[/url] .

  12. 9 Lessons Your Parents Teach You About Robotic Vacuum Cleaner On Sale robotic vacuum Cleaner on sale

  13. Thanks for finally talking about > JS 依照元素出現數量排序 – 馬老師
    雲端研究室 < Loved it!

發佈留言

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