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
aviator real money game aviator real money game .
You’ll Never Guess This Licensed Slots’s Benefits Themed Slots
20 Irrefutable Myths About Slot Symbols: Busted Slot Strategy
10 Things Everybody Gets Wrong About The Word “Birth Defect Lawsuit” Birth Defects
The Worst Advice We’ve Seen About Semi Truck Lawsuits Semi Truck Lawsuits semi truck accident Lawsuit
Overview to Imitation Premium Purses
Reasons of an Appeal for Replica Upscale Luggage
Version Designer Handbags Possess Risen In Popularity DueTo Numerous Compelling Causes
Cost Represent Commonly A Prompting Factor
Outstanding Replicas Present A Attraction About Label Labels WithNo The Substantial Expense
Advances With Manufacturing Represent Which Those Copies Commonly Have Exceptional Equivalencies To A Originals
With Them Which Value Style But Represent Budget-Conscious
Replica Luxury Purses Supply A Inviting Replacement
Advice With Spotting Validity
Scrutinizing Elements Including Logos Stitching With Accessories
Will Aid Decide An Replica’s Validity
Comparing These Features With Pictures Of Real Designer Baggage
Might Expose Several
Alternatives InPlaceOf Imitation Premium Baggage
AffordablyPriced Designer Equivalents
Be Achievable That Give Fashionable Choices WithNo Compromising With High Standards
Those Items Are Formally Built From Manufacturers Who Offer Premium Vogue With Accessible
aviator play online https://www.aviator-crash-game.ru .
https://generic-pills.ru
15 Things You Don’t Know About Window Seal Replacement glass window replacement near me
(Cathy)
aviator game download https://aviator-crash-game.ru .
Good article. I absolutely love this website. Stick with
it!
Medicines prescribing information. Long-Term Effects.
can i get phenytoin online
Some what you want to know about meds. Read information here.
aviator bonus game https://aviator-crash-game.ru/ .
online aviator game aviator-crash-game.ru .
11 “Faux Pas” That Are Actually Acceptable To Make With Your Trucking
Lawyers Truck Accident Law Firm
10 Simple Ways To Figure Out Your High Roller Slots Slot Providers, Articlement.Com,
aviator game download http://aviator-crash-game.ru .
What’s The Current Job Market For Treadmill Foldable Electric Professionals Like?
Treadmill Foldable Electric
5 Killer Quora Answers To Double Travel Stroller double travel stroller, flutelizard98.Bravejournal.net,
aviator online game http://www.aviator-crash-game.ru .
aviator game download http://www.aviator-crash-game.ru/ .
5. Erb’s Palsy Lawyers Projects For Any Budget erb’s Palsy lawsuit
17 Signs You Work With Fela Railroad Employers’ Liability Act Fela
aviator game tricks http://aviator-crash-game.ru/ .
15 Reasons To Not Ignore Uk Online Grocery Shopping Sites Air Control
Damper For 6″ Duct (vimeo.com)
A List Of Common Errors That People Do With Online Casino
Slots online slot machines
10 Things Everybody Hates About Play Slots best payouts slots
What’s The Job Market For Online Shopping Clothes Uk Cheap
Professionals Like? Online shopping Clothes uk cheap
17 Signs To Know If You Work With Slot Promotions fun Slots
I truly love your website.. Very nice colors & theme. Did you build this website
yourself? Please reply back as I’m attempting to create my very own website and would like to know where you got this from or
exactly what the theme is called. Thank you!