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,623,687 Responses

  1. Sazrufx表示:

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

    bike.by/forum/viewtopic.php?f=84&t=85801

  2. Archie表示:

    What Freud Can Teach Us About Commercial Truck Accident Attorney truck wreck lawyers (Archie)

  3. Hi, always i used to check web site posts here in the early hours in the morning, since i love to learn more and more.

  4. 10 Of The Top Facebook Pages Of All Time Electric Wall.Mounted Fire wall mounting fireplace

  5. I һave rеad some goоd stuff here. Certаinly value ƅookmarking for revisiting.
    I wonder how much attempt you place to make thiѕ kind of wonderful inflгmative web
    site.

    Feel free to visit my blog – best inmate text service

  6. 20 Things You Need To Be Educated About Private ADHD Assessments Private Adult Adhd assessment near me

  7. Can You Ride A Mobility Scooter On The Pavement Tools To Ease Your Everyday Lifethe Only Can You Ride A Mobility Scooter
    On The Pavement Trick That Every Person Should Know can you ride a mobility scooter on the pavement

  8. RileyBok表示:

    Viagra * Cialis * Levitra

    All the products you are looking seeking are currently available in support of 1+1.

    4 more tablets of identical of the following services: Viagra * Cialis * Levitra

    https://pxman.net

  9. The 9 Things Your Parents Teach You About 2 Seater Fabric Sofa Sale
    2 Seater fabric sofa sale

  10. Guide To Injury Accident Lawyers: The Intermediate Guide The
    Steps To Injury Accident Lawyers Injury accident lawyers

  11. Plumbers表示:

    I just could not leave your website prior to suggesting that I really
    loved the usual info an individual provide on your guests? Is going to be again steadily to
    check up on new posts

  12. See What Mini Replacement Key Fob Tricks The Celebs Are Using mini replacement key fob

  13. Jamika表示:

    5 Reasons To Be An Online Sports Toto Live Shop And
    5 Reasons Not To 안전놀이터 순위 (Jamika)

  14. You’re About To Expand Your Electric Fireplace Suites Freestanding Options freestanding electric fireplace Heater with mantel

  15. The 9 Things Your Parents Taught You About 2 In 1 Travel System 2 In 1 travel system

  16. slot表示:

    Ridiculous quest there. What occurred after? Thanks!

  17. This Week’s Top Stories About Mobile Car Key Cutting Mobile
    Car Key Cutting mobile Key cutting

  18. An Easy-To-Follow Guide To Choosing The Right Mesothelioma Lawyer Mesothelioma Litigation

  19. This is very attention-grabbing, You’re an excessively
    skilled blogger. I’ve joined your rss feed and sit up
    for searching for extra of your excellent post.
    Additionally, I have shared your web site in my social networks

發佈回覆給「распечатать копию кассового чека」的留言 取消回覆

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