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
10 Meetups On Counter-Strike Global Offensive You Should Attend case esports
Thanks designed for sharing such a good idea, paragraph is fastidious, thats why
i have read it completely
Here is my web blog … gold bullions
cost of propecia without insurance order cheap propecia buying propecia pill
Five Killer Quora Answers On Certified Slots certified slots
13 Things About Upvc Window Repairs You May Not Know window repairs near Me
http://finasteride.store/# propecia for sale
Your style is unique compared to other people I have read stuff from.
Thanks for posting when you have the opportunity, Guess I will just bookmark this page.
Unquestionably believe that which you stated. Your favorite justification seemed to be on the
web the simplest thing to be aware of. I say to you, I definitely get annoyed
while people consider worries that they just don’t
know about. You managed to hit the nail upon the
top and also defined out the whole thing without having
side effect , people can take a signal. Will likely be back to get more.
Thanks
ninety% of the time I buy jewelry, the shop gives it to me
in a mini zip-prime baggie. And there are many different types of public service.
Look at my website; Personalised jewelry travel organizer – ymparts.co.kr,
buy cytotec online: buy cytotec pills online cheap – buy cytotec in usa
What Is The Porsche Key Shell Term And How To Use It
program
ciprofloxacin 500mg buy online buy cipro online п»їcipro generic
buy generic ciprofloxacin: ciprofloxacin 500 mg tablet price – buy cipro online without prescription
https://cytotec.club/# Abortion pills online
My web site: facebook of sex
Misoprostol 200 mg buy online buy cytotec pills online cheap buy cytotec over the counter
5 Killer Quora Answers On Mesothelioma Personal Injury Lawyers best lawyer for mesothelioma (https://monrad-boye-2.technetbloggers.de/)
10 Tell-Tale Signs You Need To Get A New Porsche Key Fobs Replacement porsche key uk
Three Reasons Why Your Slot Sound Effects Is Broken (And How To Fix It) Slot
Wins (0522565551.Ussoft.Kr)
Abortion pills online buy cytotec pills Cytotec 200mcg price
cipro online no prescription in the usa: ciprofloxacin over the counter – cipro
The 10 Most Terrifying Things About Replacement Handles For Upvc Windows Replacement Handles for upvc Windows
7 Simple Strategies To Totally Enjoying Your Best Accident Attorney Near Me New York Accident Attorneys
Five Essential Tools Everyone Within The Window Repairman Industry Should
Be Using Window repairs
Guide To Treadmill For Home Use: The Intermediate
Guide In Treadmill For Home Use treadmill For home (seren.kr)
https://ciprofloxacin.tech/# buy cipro
casino online bono por registro sin deposito http://sin-deposito-peru.org/ .
https://nolvadex.life/# tamoxifen estrogen
cheap propecia tablets order cheap propecia prices order generic propecia no prescription
Five Qualities That People Search For In Every Auto Accident Settlement auto Accident Law firm