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
lisinopril 30 mg daily: lisinopril brand name cost – can i order lisinopril over the counter
Ten Things Your Competitors Learn About Motorcycle Accident Litigation Motorcycle Accident Attorneys
https://ciprofloxacin.tech/# ciprofloxacin
http://finasteride.store/# cost generic propecia no prescription
The Sage Advice On Ranking SEO Software From An Older Five-Year-Old suchmaschinenoptimierung software (Lurlene)
What’s The Reason Mesothelioma Law Is Fast Becoming The Hottest Fashion Of 2023 asbestos law (Aimee)
10 Meetups On Double Glazing Repairs Near Me You Should Attend Double glazing Repairs
How The 10 Worst Titration ADHD Medications Fails Of All Time Could Have Been Avoided Adhd Titration Process
https://cytotec.club/# buy cytotec pills
20 Up-Andcomers To Watch The Mental Health Psychiatrist Near Me Industry Mental Health Assessments Online
buy cipro ciprofloxacin order online buy ciprofloxacin over the counter
buy cipro online canada: buy cipro – ciprofloxacin
Find Out More About Windows And Doors Leeds When You Work
From At Home cheap
The No. 1 Question Everyone Working In CSGO New Case Must Know How
To Answer hydra case (Brittany)
buy tamoxifen: nolvadex estrogen blocker – what happens when you stop taking tamoxifen
20 Things You Should Have To Ask About GSA SER Ranker Before You Buy GSA SER Ranker buy gsa ser
Five Killer Quora Answers To Renault Clio Replacement Key Card renault Clio replacement key card
The Next Big New Shopping Online Industry lossless Audio cable – vimeo.Com –
https://finasteride.store/# generic propecia price
15 Gifts For The CSGO Most Profitable Cases Lover In Your Life Operation wildfire Case
buy cytotec over the counter: order cytotec online – buy cytotec online
Организация свадьбы под ключ https://yagodawedding.ru/ с гарантией. Быстрый расчет сметы, работаем с любым бюджетом. Берём на себя абсолютно все свадебные хлопоты. Организуем одни из самых красивых свадебных и семейных событий.
Why Combination Washer Dryer In One Isn’t As Easy As You
Think Washer dryers cheap
Misoprostol 200 mg buy online buy misoprostol over the counter buy cytotec in usa
https://finasteride.store/# buy cheap propecia online
You could certainly see your expertise within the work you write.
The sector hopes for more passionate writers like you who are not afraid to say how they believe.
At all times go after your heart.
Guide To Double Glazing Windows Repairs: The Intermediate Guide To
Double Glazing Windows Repairs double
buy cytotec purchase cytotec buy cytotec pills online cheap
generic propecia tablets propecia tablet order generic propecia pills
I do not know whether it’s just me or if everyone else encountering issues with your site.
It appears as if some of the text within your content are running off the screen. Can somebody else please comment and let me know if this is happening to them too?
This could be a issue with my web browser because I’ve had this happen before.
Appreciate it