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
Greetings! Very helpful advice within this article! It’s the little
changes that will make the most significant changes.
Many thanks for sharing!
It’s an remarkable piece of writing in favor of all the online viewers; they will take benefit from it I am sure.
10 Things You Learned In Preschool That’ll Help You Understand Couches Near Me For Sale Cheap Couches For Sale Under $200
10 Things We Are Hating About Motor Vehicle Legal motor Vehicle Accident
Excellent goods from you, man. I’ve understand your stuff previous to and you’re just extremely excellent.
I really like what you’ve acquired here, certainly like what you’re stating and the way in which you say it.
You make it enjoyable and you still care for to keep it sensible.
I can not wait to read far more from you. This is actually a tremendous site.
Greetings! I ҝnow thіѕ is kіnd of off topic Ƅut I ᴡɑs wondering іf yyou knew
wheге I cߋuld get a captchua plgin fοr
my cօmment form? І’m using the same blog platform аs yoᥙrs and I’m havіng pгoblems finding οne?
Thanks a lot!
Feel free to visit my webpage – Slot Gacor
Where Is Green Power Mobility Scooters One Year From Now? green power mobility scooter reviews
Five Killer Quora Answers To Popular Casino Slots popular casino slots
With havin so much content and articles do
you ever run into any issues of plagorism or copyright infringement?
My site has a lot of unique content I’ve either written myself or outsourced but it
seems a lot of it is popping it up all over the internet
without my permission. Do you know any solutions to help prevent content from being stolen? I’d really
appreciate it.
where to buy macrobid price
can i buy prednisone without dr prescription
Treatment For ADD Isn’t As Difficult As You Think adhd
treatment london, Arleen,
Five People You Need To Know In The Online Casino Slots Industry
Top winning slots
15 Boot Mobility Scooters Benefits You Should All
Know Arlen Nizo
After a long struggle and a long period of legal action,
asbestos legal measures resulted in the partial ban of 1989 on the manufacturing,
processing, and distribution of a majority of Asbestos compensation-containing products.
This ban remains in effect.
Ten Things You Learned In Kindergarden That Will Help You Get Window Companies Bedford flush casement window bedfordshire (Hubert)
can you buy generic atarax tablets
5 Killer Quora Answers On Window Replacement Near Me
window replacement near me
10 Tell-Tale Warning Signs You Should Know To
Get A New Unique Slots high rtp slots (p3terx.com)
Wow that was odd. I just wrote an very long comment but after I clicked
submit my comment didn’t show up. Grrrr… well I’m not writing all that over again.
Anyway, just wanted to say great blog!
10 Things That Your Family Taught You About
Upvc Window Repairs Near Me upvc window repairs Near me
The 10 Most Terrifying Things About Hire Truck Accident
Lawyer truck accident
The 10 Most Scariest Things About Dangerous Drugs
Lawyers dangerous drugs lawyer
What’s The Current Job Market For Window Repair Near Me Professionals Like?
window repair near me (http://fgks.org/proxy/index.php?q=ahr0cdovl25ldhzvew5llnj1l3vzzxivym9iy2f0zm9hbtiv/cgfnzt1vc2vyugfnzwrlbg9yzxn2ywxszwf4bgt4egjx)
Beware Of This Common Mistake With Your Sofas Leather genuine leather sofa (image.google.sh)
Who’s The World’s Top Expert On 4 Wheel Disability Scooter?
4 wheel enclosed mobility Scooter
8 Tips To Up Your Motor Vehicle Lawsuit Game Motor vehicle Accident lawsuit
An In-Depth Look Into The Future What Will The Auto Accident Lawyer Industry Look Like
In 10 Years? auto accident law firms
See What Can I Buy From A Uk Website Tricks The Celebs Are Utilizing can i buy from a uk
website (Felipa)
10 Things That Your Family Taught You About France Online Shopping Sites Clothes
France Online Shopping Sites Clothes (https://The-Challenger.Ru/Goto/AHR0CHM6Ly92AW1Lby5Jb20VOTMxODczODg3)