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
Demo Slot Sugar Tools To Ease Your Daily Life
Demo Slot Sugar Trick That Should Be Used By Everyone Learn demo Slot sugar
Medication information for patients. Brand names.
how can i get cheap levitra price
Best trends of meds. Get information here.
Hello superb website! Does running a blog similar to this require
a great deal of work? I’ve no understanding
of computer programming however I was hoping to start
my own blog soon. Anyway, if you have any suggestions or techniques for new blog owners please share.
I know this is off subject nevertheless I simply needed to ask.
Many thanks!
Feel free to visit my web page – fun88
Highly descriptive post, I liked that a lot. Will there be
a part 2?
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.
10 Healthy Online Clothing Sites Uk Habits Air Hose Reel With Bracket
These Are The Most Common Mistakes People Make With Car Accident Attorneys car accident law firm
The 10 Scariest Things About High Roller Slots high roller slots (https://peatix.com/user/22256015)
Your article has given me inspiration to find free money. I was really helped by your content, if you want to make free money just like me, please visit my website, I dissected several of the articles above on my website
It’s really a great and useful piece of information. I’m happy
that you simply shared this useful info with us.
Please stay us up to date like this. Thanks for sharing.
my website … اجاره خودرو در دبی
hey there and thank you for your information – I have definitely picked up
something new from right here. I did however expertise some technical points using this website, as I experienced to reload the web site a lot of times
previous to I could get it to load correctly. I had been wondering if your
web host is OK? Not that I’m complaining, but slow loading instances times will often affect your
placement in google and can damage your high quality
score if advertising and marketing with Adwords. Anyway I am adding this RSS to my email and can look
out for much more of your respective interesting content.
Ensure that you update this again soon.
20 Things Only The Most Devoted New Slots Online Fans Are
Aware Of slots with free bonus rounds (Alethea)
What Experts In The Field Of Online Shopping Figures Uk Want
You To Be Able To Optical Grade Glass Magnifier
The Reasons Why Examples Of Online Products Has Become Everyone’s Obsession In 2023 Adjustable Adirondack Chair
can i order cheap prednisone prices
I always emailed this webpage post page to all
my contacts, because if like to read it afterward my links will
too.
A Sage Piece Of Advice On Examples Of Online Shopping From An Older Five-Year-Old Premium Longboard Skateboarding
The 10 Most Terrifying Things About High Roller Slots High Roller Slots
יתרונות: ראשית כל הן מאפשרות לכם לבלות בילוי ספונטני, שנית ניתן ליהנות מהשירותים שלהן בכל זמן ובכל מקום והן גם מעסות מקצועיות. שמנסות להחזיר בבת אחת את התענוגות של הגברים שהן מכירות, ולאחר מכן חוזרות על ההנאות הרבה פעמים. עיסוי אירוטי בחדרה עם מעסות הללו נערות ליווי פרטית
Get Rid Of Slot Competitions: 10 Reasons Why You Don’t Need
It top jackpot slots (images.google.com.My)
7 Things About Shopping Online Sites List You’ll Kick Yourself For Not Knowing
shopping online uk websites
This Week’s Top Stories About Slot Bonuses modern slots; appc.Cctvdgrw.Com,
Hey There. I found your blog using msn. This is a really well written article.
I’ll make sure to bookmark it and come back to read more of your useful information.
Thanks for the post. I’ll definitely comeback.
My webpage … https://www.cucumber7.com/
The History Of Shopping Online handheld paint Mixer
I am extremely impressed along with your writing abilities and also with the format for your blog.
Is that this a paid theme or did you modify it your self?
Either way keep up the nice high quality writing,
it is rare to peer a great weblog like this one these days..
Feel free to surf to my web site; find sex near me
11 Ways To Completely Sabotage Your Online Retailers
Uk Stats Heavy-Duty Appliance Pull
This Is The Ultimate Guide To Replacement Windows Luton Window repair near Me
Livetotobet adalah situs news berita olahraga sepakbola terupdate dengan menyajikan fakta seputar dunia olahraga sepakbola semoga informasi yang diberikan oleh
live totobet dapat bermanfaat untuk penggemar berita sepakbola dunia
I was recommended this web site by means of my cousin. I’m
no longer sure whether or not this submit is written through him
as no one else realize such specific approximately my trouble.
You’re amazing! Thank you!
Feel free to surf to my blog post :: جک دو ستون