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,210,838 Responses

  1. DominicJaw表示:

    mexico drug stores pharmacies: medication from mexico pharmacy – purple pharmacy mexico price list

  2. Because the admin of this web page is working, no uncertainty very quickly it will be well-known, due to
    its feature contents.

  3. DominicJaw表示:

    buying from online mexican pharmacy: п»їbest mexican online pharmacies – medication from mexico pharmacy

  4. Nelsonbluck表示:

    buying from online mexican pharmacy: п»їbest mexican online pharmacies – pharmacies in mexico that ship to usa

  5. WayneCoirl表示:

    mexican border pharmacies shipping to usa: mexico pharmacies prescription drugs – mexican mail order pharmacies

  6. Abby表示:

    It’s going to be finish of mine day, except before finish I am reading this great piece of writing
    to increase my know-how.

  7. 10 Tips For Getting The Most Value From Replacement Key For Peugeot Partner Van peugeot 108 replacement key cost

  8. 公司登記表示:

    https://accounting1016.s3.us-west-004.backblazeb2.com/research/accounting1016-(153).html
    Following these simple tips are certain to make the process go smoothly and efficiently.

  9. Arnoldten表示:

    mexico drug stores pharmacies mexican border pharmacies shipping to usa mexican drugstore online

  10. Cerys表示:

    The 10 Most Terrifying Things About Futon Mattress Double futon mattress
    double (Cerys)

  11. 20 Up-Andcomers To Watch The Patio Door Repair Company Industry
    patio door repair company Near me (noel-stewart-2.blogbright.net)

  12. 10 Of The Top Mobile Apps To Fiat 500 Key Replacement fiat 500 duplicate Key

  13. Arnoldten表示:

    mexican online pharmacies prescription drugs buying prescription drugs in mexico medicine in mexico pharmacies

  14. 大里 整骨表示:

    https://ya-drbogin-15.syd1.digitaloceanspaces.com/how-massage-therapy-enhances-postpartum-recovery.html
    It is customary to keep away from wearing white in your children’s wedding day.

  15. WayneCoirl表示:

    mexico drug stores pharmacies: reputable mexican pharmacies online – purple pharmacy mexico price list

  16. The Guide To Slot Apps In 2023 real Money Slots

  17. sandibet表示:

    Good day! I could have sworn I’ve been to this website before
    but after looking at a few of the posts I realized it’s new
    to me. Anyways, I’m definitely pleased I discovered
    it and I’ll be book-marking it and checking back regularly!

  18. Five Laws That Will Aid In The Butt Plugs Online Store Industry Large vibrating butt plug

  19. Forexmob.Ru表示:

    How Twilight Princess Has Changed The History Of Twilight Princess Gates Of Princess Demo Slot, Forexmob.Ru,

  20. Nelsonbluck表示:

    mexican border pharmacies shipping to usa: buying prescription drugs in mexico online – pharmacies in mexico that ship to usa

  21. WayneCoirl表示:

    mexican online pharmacies prescription drugs: purple pharmacy mexico price list – п»їbest mexican online pharmacies

  22. Arnoldten表示:

    reputable mexican pharmacies online mexican online pharmacies prescription drugs mexico drug stores pharmacies

  23. DominicJaw表示:

    п»їbest mexican online pharmacies: mexican pharmaceuticals online – mexico pharmacies prescription drugs

  24. 舒壓課程表示:

    https://tendoncare12.s3.us-east-005.backblazeb2.com/research/tendoncare12-(402).html
    Reviewers describe it as very slimming and say the beadwork is exquisite.

  25. WayneCoirl表示:

    buying from online mexican pharmacy: mexican online pharmacies prescription drugs – mexican rx online

  26. Replacement Double Glazed Glass Only Near Me Tools To Streamline Your Day-To-Day Life Replacing Double Glazed Glass

發佈留言

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