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
In this case, the life insurance policy company’s cash.
Feel free to surf to my website: Bookmarks
наркологическая срочная помощь наркологическая срочная помощь .
Some individuals plant based vape canada flavored pure nicotine
Allow me reveal you in my testimonial of NewULife.
Here is my web site: ranch wedding venues texas
I’ll right away take hold of your rss feed as I can’t find your
e-mail subscription link or e-newsletter service.
Do you have any? Please allow me recognize in order
that I may subscribe. Thanks.
I don’t even know the way I ended up here, however I believed this post was once great.
I don’t know who you are however definitely you are going to
a famous blogger if you aren’t already. Cheers!
Some people plant based vape flavorful pure nicotine
The vapor and taste high quality is likewise wonderful.
my blog :: Bookmarks
There are numerous root causes of childish what to do if baby has infantile spasms.
NewULife Ranks. Welcome to my NewUlife Evaluation.
Also visit my web page :: home theater projector rca
There are a number of root causes of childish convulsions.
Here is my blog post how rare is infantile spasms
They have products to sell or solutions to provide.
Have a look at my homepage :: ceramics classes near me for adults
Let me reveal you in my evaluation of NewULife.
Here is my blog post :: what to wear to a texas ranch wedding
Allow me reveal you in my review of NewULife.
Feel free to surf to my blog what to wear to a texas ranch wedding
10 Things We Love About Sectional L Shaped Sofa Recliner l Shape sofa
USP human development hormonal agent (somatropin).
Here is my web-site :: ceramic pottery painting for sale
I’ve been surfing online greater than 3 hours as of late, yet I by no
means discovered any interesting article like yours.
It’s pretty price sufficient for me. In my opinion, if all web owners and bloggers made excellent content
as you probably did, the internet will be a lot more useful than ever before.
NewULife Ranks. Invite to my NewUlife Testimonial.
Visit my page :: bookmarks
IVC Дизайн человека https://vkl-design.ru Дизайн человека. 1/3 Дизайн человека.
The bigger Slim battery, terrific for longer runtimes.
Feel free to visit my site Foil Drive Assist Max
In this instance, the life insurance company’s loan.
Also visit my web site hss shoulder surgeons nyc
Real HGH Rewards (NewULife Hgh Gel Not Examined Yet).
Here is my website cheap farm wedding venues texas
best pharmacy prices viagra: viagra pharmacy coupons – mexican pharmacy cipro
I’m amazed, I have to admit. Seldom do I encounter a blog that’s both educative and entertaining,
and without a doubt, you’ve hit the nail on the head.
The issue is something too few folks are speaking
intelligently about. Now i’m very happy that I came across this during my search for something concerning this.
A synthetic USP human development hormone (somatropin).
my blog – home theater seating layout
There are numerous sources of infantile spasms Causes spasms.
10 Windows And Doors Maidstone-Related Projects To Stretch Your Creativity aluminium Front Doors Maidstone
What’s Everyone Talking About Adhd Assessment Adult This Moment Adhd assessment for adults Leicester
Mobile vaporizers make use of batteries as a power source.
my web-site :: plant based vape uk
USP human growth hormonal agent (somatropin).
Here is my page – Home theater projector Rca 1080P