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
sweet bonanza oyna sweet bonanza oyna sweet bonanza
deneme bonusu veren casino siteleri: Deneme Bonusu Veren Siteler – deneme bonusu veren casino siteleri
Do not miss out on the opportunity to uncover Washington DC like never prior to along with a private metropolitan area scenic tour private limo service
guvenilir slot siteleri: slot oyunlar? – az parayla cok kazandiran slot oyunlar?
If you have not attempted a Brazilian steakhouse yet, you’re losing out! The limitless skewers of completely prepared meat are just divine. Take a look at my thoughts at brazilian bbq restaurant near me
Casino Siteleri Canl? Casino Siteleri deneme bonusu veren casino siteleri
Whoa tons of wonderful material! https://maps.google.gl/url?q=https://betflix22-th.me
Real savings made if you buy on this site. Cheap zantac for nausea . Should I call my doctor?
Because the admin of this web page is working, no doubt very shortly it will be well-known, due to its feature contents. http://Energypop.Co.kr/gnuboard5/bbs/board.php?bo_table=qna&wr_id=227889
оплатить заказ можно при помощи банковской карты либо посредством прямого платежа в https://tesera.ru/user/gehthnfdt Индию.
casino bahis siteleri casino bahis siteleri casino bahis siteleri
This was quite informative. For more, visit Mesa Roofing
What’s The Job Market For Asbestos Lawsuit Settlement Amount Professionals Like?
Asbestos lawsuit – https://securityholes.science/wiki/how_to_Create_successful_asbestos_class_action_litigation_instructions_for_homeschoolers_from_home,
en guvenilir casino siteleri casino en iyi siteler guvenilir casino siteleri
http://denemebonusuverensiteler25.com/# deneme bonusu veren yeni siteler
Gentlemen’s Club 부산달리기
Kalau ada pesan yang minta kamu klik link dan minta data, jangan langsung percaya.
Bisa jadi itu cuma trik buat dapatin informasi pribadimu.
Pastiin semuanya aman dulu!.
I enjoyed this post. For additional info, visit skilled plastic surgeon
If you would like to obtain a good deal from this article then you have to apply these strategies
to your won website.
https://sweetbonanza25.com/# sweet bonanza kazanma saatleri
deneme bonusu veren siteler deneme bonusu veren siteler yeni denemebonusuverensiteler25
http://slotsiteleri25.com/# guvenilir slot siteleri
выведение из запоя на дому выведение из запоя на дому .
срочный вывод из запоя ростов http://vyvod-iz-zapoya-rostov229.ru/ .
sweet bonanza giris: sweet bonanza kazanma saatleri – sweet bonanza slot
bilinmeyen bahis siteleri canl? casino siteleri casino bahis siteleri
https://sweetbonanza25.com/# sweet bonanza slot
Superb, what a blog it is! This weblog provides useful data
to us, keep it up.
Avoid the hassle of airport parking with our convenient and affordable long-term parking options at O’Hare International Airport (ORD). Book online and enjoy a stress-free travel experience best long term parking at ord
https://sweetbonanza25.com/# sweet bonanza oyna