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
The author is called Roseline. For years she’s been working
regarding interviewer but she’s always wanted her business.
As a man what he really likes is doing interior design but he doesn’t
offer the time as of late. My wife and I live in New Shirt.
http://slotsiteleri.bid/# slot siteleri guvenilir
Indisputable Proof You Need Coffee Beans Near Me coffee Bean coffee
Howdy! Would you mind if I share your blog with my twitter group?
There’s a lot of people that I think would really appreciate your content.
Please let me know. Many thanks
15 Things You Didn’t Know About Leather Couch reupholster leather couch [Williams]
slot siteleri guvenilir: deneme bonusu veren slot siteleri – en guvenilir slot siteleri
The 9 Things Your Parents Taught You About Bi Fold Door Repair Near Me bi fold door repair (Esmeralda)
It’s wonderful that you are getting ideas from this article as well as from our dialogue made at this place.
Hmm is anyone else experiencing problems with the images
on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog.
Any responses would be greatly appreciated.
Bed Settee For Sale Tools To Make Your Everyday Lifethe Only Bed Settee For Sale Trick That Every
Person Must Be Able To bed settee for sale (Joanne)
The People Nearest To ADHD Online Test Tell You Some Big Secrets
Female Adhd Test Free – Genebiotech.Co.Kr,
5 Killer Quora Answers On Treadmills UK treadmills uk
http://sweetbonanza.network/# sweet bonanza 90 tl
http://slotsiteleri.bid/# slot oyun siteleri
5 Killer Quora Answers To Test For ADHD In Adults test for adhd
in adults (https://ordermall.Co.kr)
A Look At The Ugly Real Truth Of Best Accident
Lawyers accident Lawyers Near Me
The Three Greatest Moments In Oil Filled Wall Mounted Radiators
History Oil filled electric Radiators
Look at my web page … Explore Daycares Locations (Katie)
Salutations, companion reader. I must applaud the author for their discerning and eloquently-crafted blog
post. The subject was both insightful and introspective, leaving me with
a more nuanced understanding of the issue
at hand.
I would desire to extend an recommendation to become a member of the
renowned PUSSY888 organization. This space offers a panorama of fun and stimulation, suiting those who cherish
the more elegant things in reality. I urge you to venture
into the multifaceted selections and immerse yourself in the exciting explorations that summon you.
Your participation would be extremely welcomed, and I anticipate with
enthusiasm the opportunity to engage with you more intimately within this distinguished web-based landscape
Also visit my web-site; online casino player behavior analytics
14 Clever Ways To Spend On Leftover Nissan Juke Key Replacement Budget services
I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do
it for you? Plz respond as I’m looking to create my
own blog and would like to find out where u got this from.
appreciate it
9 Things Your Parents Taught You About Bifold Door Repair bifold door Repair
Five Lessons You Can Learn From Mesothelioma Law Firm Mesothelioma Lawyer
I like looking through an article that can make men and women think.
Also, thank you for allowing for me to comment!
When someone writes an piece of writing he/she keeps the image of a user in his/her
mind that how a user can know it. Therefore that’s why this article is outstdanding.
Thanks!
10 Things You Learned In Kindergarden That’ll Help You With Treadmill With Incline
Uk Under Bed treadmill with incline – big.lordfilm-s.club,
en yeni slot siteleri: guvenilir slot siteleri – slot kumar siteleri
The 9 Things Your Parents Teach You About Lost Lexus Key Fob lost Lexus key
Why Is There All This Fuss About Male Depression Symptoms?
major Depressive symptoms
Hello there, just became aware of your blog through Google, and found that it’s really informative.
I’m gonna watch out for brussels. I’ll be grateful if you continue
this in future. Lots of people will be benefited from your writing.
Cheers!