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
Wow that was unusual. I just wrote an really long comment
but after I clicked submit my comment didn’t appear. Grrrr…
well I’m not writing all that over again. Anyhow, just
wanted to say excellent blog!
What Is How Anxiety Is Treated And Why Is Everyone Speakin’
About It? natural anti anxiety Treatment
buying prescription drugs in mexico mexico drug stores pharmacies mexican online pharmacies prescription drugs
15 Reasons Not To Overlook Toto4d Toto Online Terbaik
https://al-esnad.com.sa/blog/2024/09/18/arenda-kvartir-na-sutki-na-karte-v-grodno-11/
Howdy! I know this is somewhat 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 problems finding one?
Thanks a lot!
My web page :: Mostbet BR
http://mexicanpharm24.pro/# mexico pharmacies prescription drugs
reputable indian online pharmacy
What’s up Dear, are you in fact visiting this web site on a regular basis, if so afterward you will definitely take fastidious knowledge.
Godfather789ยเว็บตรงของคนไทย เจ้าพ่อมาเฟียเว็บใหญ่ไม่มีโกง
Very rapidly this web page will be famous among all blog visitors,
due to it’s fastidious content
my web page – Erotikanzeigen
erection pills viagra online ways to treat erectile dysfunction 100mg viagra without a doctor prescription
Are You Responsible For The Electric Rollator Walker Budget?12 Top Ways To Spend Your Money designer rollator
One Key Trick Everybody Should Know The One Asbestos Attorneys Near Me Trick Every Person Should Know asbestos lawyer – Wikimapia.org –
https://drugs24.pro/# ed solutions
mail order pharmacy india
What’s The Current Job Market For Rolls Royce Replacement Key Professionals?
rolls royce replacement key [jasontrout1.bravejournal.net]
10 Meetups On Window Hinge Repair You Should Attend double glazing hinge Repairs near me
At this moment I am going away to do my breakfast, afterward having my breakfast coming again to
read further news.
The Reasons Anxiety Symptoms Lump In Throat Isn’t As Easy As You Imagine Anxiety Symptoms Headache
Everything is very open with a very clear description of the
issues. It was truly informative. Your site is very useful.
Thank you for sharing!
Outstanding story there. What occurred after? Take care!
Five Killer Quora Answers On Private ADHD Assessment London private adhd assessment london
Why Private ADHD Assessment Is The Right Choice For You?Private adhd Assessment kent
vacuum pumps for ed canadian drugs prescription drugs
cheapest online pharmacy india top 10 pharmacies in india india pharmacy mail order
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
landik-diploms.ru
Профессиональный сервисный центр по ремонту сотовых телефонов в Москве.
Мы предлагаем: ремонт ультрабуков мастер
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
See What Replacement Car Key Audi Tricks The Celebs Are Using replacement car key
audi (https://images.google.com.gt)
15 Secretly Funny People Working In Skoda Fabia Key Replacement Skoda keys (https://hvass-riis.blogbright.net/)
Indisputable Proof You Need Porsche Key Battery near Me
http://indianpharmdelivery.com/# indian pharmacy
buy medicines online in india