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,153,582 Responses

  1. What Is Malpractice Claim And Why Is Everyone
    Dissing It? Malpractice Lawsuit

  2. Awesome! Its genuinely awesome paragraph, I have got much clear idea concerning from this piece
    of writing.

  3. The Worst Advice We’ve Heard About Slot Rewards top jackpot Slots

  4. CedricPreer表示:

    Lionel Messi https://lionelmessi.prostoprosport-br.com e um jogador de futebol argentino, atacante e capitao do clube da MLS Inter Miami. , capitao da selecao argentina. Campeao mundial, campeao sul-americano, vencedor da Finalissima, campeao olimpico. Considerado um dos melhores jogadores de futebol de todos os tempos.

  5. See What Online Charity Shop Uk Clothes Tricks The Celebs Are Using online Charity shop Uk clothes

  6. elsycrays.top表示:

    The Story Behind Lock Smith For Cars Will Haunt You For The Rest Of Your Life!
    elsycrays.top

  7. A Time-Travelling Journey: How People Talked About
    Truck Accidents Attorney 20 Years Ago Truck Accident Law Firms

  8. 15 Shocking Facts About ADHD Diagnosis The Words You’ve Never
    Learned get diagnosed for adhd

  9. SEO_giMa表示:

    продвижение сайтов в москве яндекс prodvizhenie-sajtov-v-moskve115.ru .

  10. SEO_qyMa表示:

    продвижение сайтов по москве в топ prodvizhenie-sajtov-v-moskve115.ru .

  11. Howdy! I know this is kind of off topic but I was wondering if you knew where
    I could locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m
    having trouble finding one? Thanks a lot!

  12. A How-To Guide For Motor Vehicle Settlement
    From Beginning To End Motor Vehicle Accidents

  13. How Semi Truck Became The Top Trend In Social Media Semi truck accident law Firm

  14. See What Where To Buy Electronics Online Tricks The Celebs Are
    Making Use Of where to buy electronics Online

  15. It’s a shame you don’t have a donate button! I’d without a doubt donate to this excellent blog!
    I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google
    account. I look forward to fresh updates and will share this website with my Facebook group.
    Chat soon!

    Also visit my web-site; the growth matrix reviews

  16. 11 “Faux Pas” That Are Actually OK To Make With Your Slot Games Popular online slots

  17. 25 Surprising Facts About Bonus Slots slot Volatility

  18. Velma表示:

    10 Things Everybody Hates About Shopping Online Shopping
    Online shopping online sites clothes (Velma)

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

  20. Why You Should Concentrate On Improving Cerebral Palsy Law Cerebral Palsy Lawyer

  21. v9bet20943表示:

    Heya i am for the first time here. I found this board and I find It really useful & it helped me
    out much. I hope to give something back and aid others like you helped me.

  22. SEO_rpMa表示:

    продвижение сайтов в москве тарифы prodvizhenie-sajtov-v-moskve115.ru .

  23. Play Slots表示:

    Slots For Fun: The Ugly Truth About Slots For Fun Play Slots

  24. SEO_uqMa表示:

    создание и продвижение сайтов в москве я топ сайт http://prodvizhenie-sajtov-v-moskve115.ru/ .

  25. SEO_tzMa表示:

    продвижение сайтов в москве гугл https://prodvizhenie-sajtov-v-moskve115.ru .

  26. How To Survive Your Boss On Birth Defect Legal birth Defect Law Firms

  27. Here’s A Few Facts About Licensed Slots. Licensed Slots Themed slot games

  28. Willard表示:

    Buying Online From Uk To Ireland Tools To Improve Your
    Daily Lifethe One Buying Online From Uk To Ireland Trick Every
    Individual Should Know buying online from uk to ireland (Willard)

  29. SEO_piMa表示:

    seo продвижение сайтов в москве агентство http://prodvizhenie-sajtov-v-moskve115.ru/ .

發佈留言

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