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,523,974 Responses

  1. See What Cheapest Automatic Folding Mobility Scooter Tricks The
    Celebs Are Using cheapest automatic folding Mobility Scooter

  2. LeroyBak表示:

    indian pharmacies safe India pharmacy international mail order pharmacy india

  3. The 10 Most Scariest Things About Best Infant Cribs best
    infant cribs (https://Www.google.com.Pe/)

  4. Replacement Audi Key 10 Things I Wish I’d Known In The Past replacement audi keys

  5. How Birmingham Window Repair Became The Hottest Trend In 2023 Door Repair Birmingham

  6. Jav Sub Indo表示:

    I was suggested this website by my cousin. I am no longer positive whether
    or not this put up is written by way of him as no one else know such
    precise about my difficulty. You are amazing! Thank you!

  7. AbecPaish表示:

    People realized that by searching for pharmacy warfarin clinic today!

  8. Noreen表示:

    Think You’re Cut Out For Doing Fireplace Suite? Take This Quiz electric fireplace with wood mantel (Noreen)

  9. audi tt Key表示:

    9 . What Your Parents Taught You About Audi Tt Key audi tt Key

  10. Mack表示:

    Guide To Skoda Key Fob Replacement: The Intermediate Guide Towards Skoda Key Fob Replacement
    skoda key fob replacement (Mack)

  11. 20 Things That Only The Most Devoted Frost Free Fridge Freezer 50/50 Fans Should Know Extra tall fridge freezer 50/50

  12. Diplomi_ucOl表示:

    купить диплом итмо [url=https://diplomdarom.ru/]diplomdarom.ru[/url] .

  13. Trefgbu表示:

    Покупка диплома о среднем полном образовании: как избежать мошенничества?

    plastdet.ru/index.php?topic=377.new#new

  14. All The Details Of Audi Car Key Dos And Don’ts audi key replacement cost, http://79bo.com/space-uid-6820349.html,

  15. 5 Killer Quora Answers To Cost Of Tilt And Turn Upvc Windows cost of tilt and turn upvc windows

  16. What Mesothelioma Law Experts Want You To Learn Mesothelioma law expert

  17. Julianne表示:

    The One Pragmatic Trick Every Person Should Be Aware Of 라이브 카지노
    (Julianne)

  18. car repair表示:

    I go to see each day a few web pages and blogs to read articles, except this webpage gives quality based articles.

  19. Amazing! This blog looks just like my old one! It’s on a entirely different subject but it has pretty much the same layout and design.
    Excellent choice of colors!

  20. BrandonSaisy表示:

    cheap drugs: low cost prescription – drug prices

  21. Why Nobody Cares About Porsche Macan Key Replacement Cost New Porsche Key Cost

  22. BrandonSaisy表示:

    best natural ed treatment: cheaper medications – prescription drugs online

  23. Freestanding Electric Fireplace Techniques To Simplify Your Daily Life
    Freestanding Electric Fireplace Trick Every Person Should Learn freestanding electric fireplace

  24. slot gacor表示:

    Very energetic article, I enjoyed that bit.
    Will there be a part 2?

  25. Will Virtual Mystery Boxes One Day Rule The World?
    mystery box Website

  26. 15 Things You’re Not Sure Of About Online Mystery Boxes mystery Box website

  27. Diplomi_ymel表示:

    купить диплом производство man-diploms.ru .

發佈留言

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