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
how much is disability in ontario this
instance, the life insurance company’s money.
I’m the proprietor operator of GOPARTYBUS party bus hire prices perth.
The Complete List Of Online Shopping Uk Electronics Dos
And Don’ts Stylish Bird Feeders For Garden
The most effective developing editors get on Reedsy.
Here is my web blog: book publishing internships uk
Drug information for patients. Brand names.
where to get generic mobic without prescription
Everything about meds. Read here.
Meds information sheet. Drug Class.
where to buy cheap warfarin without prescription
Some news about medicine. Read information now.
An In-Depth Look Back What People Talked About Veterans
Disability Legal 20 Years Ago mahanoy city veterans disability lawyer
Hi! I know this is kinda off topic however I’d figured I’d ask.
Would you be interested in exchanging links or maybe guest writing a blog article or vice-versa?
My blog covers a lot of the same subjects as yours and I think we could greatly benefit from each other.
If you are interested feel free to shoot me an e-mail.
I look forward to hearing from you! Fantastic blog by the way!
Pills information leaflet. Cautions.
where can i get cheap cipro for sale
Everything about pills. Read here.
Hello, after reading this awesome paragraph i
am as well cheerful to share my know-how here with colleagues.
Fine way of telling, and good post to take information on the topic of my presentation topic, which i am going to deliver in academy.
The IQ Is Vape Juice Vegan the
very best completely dry natural herb vape I have attempted.
Sectional Couches For Sale Tools To Ease Your Everyday Lifethe Only Sectional
Couches For Sale Trick Every Individual Should
Learn L couches for Sale (pickmein.kr)
NewULife Ranks. Welcome to my NewUlife Evaluation.
Here is my web-site; Ai headshot generator free trial
Hi are using WordPress for your blog platform? I’m new to the
blog world but I’m trying to get started and create my own. Do you need
any html coding knowledge to make your own blog?
Any help would be really appreciated!
Good day! Do you know if they make any plugins to safeguard against hackers?
I’m kinda paranoid about losing everything I’ve worked
hard on. Any tips?
I enjoy looking through an article that can make people
think. Also, thanks for permitting me to comment!
Do you have a spam problem on this site; I also am a blogger, and I was curious about your situation; we
have created some nice methods and we are looking to swap techniques with other
folks, why not shoot me an email if interested.
NewULife Ranks. Welcome to my NewUlife Testimonial.
My homepage … ai headshot generator reddit
Some Wisdom On Upvc Window Repairs From The Age Of Five window repairs near me (Elena)
They have items to market or services to offer.
Take a look at my web page; ai profile picture generator reddit
The vapor and flavor high quality is likewise terrific.
My site; Bookmarks
Zo zullen er een stuk minder takken bladeren op je dak terechtkomen en zogen voor schade.
оборудование для конференц зала купить оборудование для конференц зала купить .
конференционный зал https://www.oborudovanie-konferenc-zalov11.ru .
Yes! Finally something about baskılı poşet.
5 Killer Quora Answers On Keys For Mercedes Genuine mercedes key fob
система для конференц зала https://oborudovanie-konferenc-zalov11.ru .
конференц зал оборудование конференц зал оборудование .
They have items to market or services to use.
Also visit my website – Bookmarks