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
where to get cheap astelin without dr prescription
10 Basics To Know Slot Payouts You Didn’t Learn In School Jackpot Slots
This History Behind Truck Accident Claim Can Haunt You Forever!
truck accident lawyers (Raina)
Drug prescribing information. Short-Term Effects.
can you get zyban pill
Everything what you want to know about drug. Read information here.
5 Killer Quora Answers To Window Glass Replacement Near
Me Window
buying cheap elimite online
Medicament prescribing information. Drug Class.
can i order cheap azathioprine
Actual information about drug. Read here.
15 Things You’ve Never Known About Window Repair Near double glazed window repairs near me
(animeportal.cl)
10 Tell-Tale Symptoms You Need To Find A New Mesothelioma Lawyer mesothelioma claims
5 Laws That’ll Help The Best Buy Mobility Scooters
Industry The Best Mobility Scooter
Medicines information. Cautions.
where to buy tamoxifen without insurance
All trends of meds. Read information here.
The 10 Scariest Things About Slot Promotions Slot
Promotions (21.farcaleniom.Com)
The Three Greatest Moments In High Limit Slots History Fun Slots
xxx
lesbian porn
This is nicely said. .
Witam serdecznie społeczność forum i na początek chciałbym was zapytać o to jakie portale internetowe goszczą najczęściej w waszych przeglądarkach Jakie są wasze preferencje w tej kwestiii ?
Ja np. odwiedzam agregator newsów z różnych portali którego jestem skromnym administratorem co go wyróżnia od innych witryn tego typu ? sami sprawdzcie i oceńcie 🙂
The 10 Most Scariest Things About Uk Online Shopping Sites For Electronics uk online shopping Sites for electronics
Unquestionably believe that which you said. Your favorite justification seemed
to be on the net the easiest thing to be aware of. I say to you, I definitely get irked while people consider worries
that they plainly don’t know about. You managed to hit the
nail upon the top and defined out the whole thing without having side-effects , people can take
a signal. Will likely be back to get more. Thanks
buying generic sildigra no prescription
15 Presents For The Workers Compensation Attorneys Lover In Your Life
lawsuits
17 Reasons To Not Ignore Double Glazing Near Me Double
Glazed Windows Repairs [Artrecord.Kr]
Learn About Upvc External Doors When You Work From The Comfort Of Your Home replacing a upvc door panel
Hey I know this is off topic but I was wondering if you
knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some
time and was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy reading your blog
and I look forward to your new updates.
звуковое оборудование для конференц зала звуковое оборудование для конференц зала .
What Are The Biggest “Myths” About Slots For Fun Could Actually Be Accurate casino slot tournaments (Augusta)
Thanks for yοur personal marvelous posting!
І aсtually enjoyed reading іt, you might bе a ɡreat author.Ӏ
will ensure that I bookmark үoᥙr blog ɑnd will ⲟften сome back somеtime soon.
Ӏ want tⲟ encourage үoս continue уοur great writing, hɑᴠе a nice evening!
Appreciate the recommendation. Let me try it out.
The Slot Reviews Case Study You’ll Never Forget Mobile Slots
The Repair Double Glazing Window Success Story You’ll Never
Believe http://www.257634.xyz