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
Here is my blog post … hookups near me
This Week’s Best Stories About Online Shopping Sites Clothes Cheap Milligram Scale (Lara)
My page … https://sui-wallet.io/
See What SEO Tools UK Tricks The Celebs Are Making Use Of seo tools uk (Sherita)
Amazing lots of terrific information!
10 Facts About Slot Sites That Can Instantly Put You In A Good Mood best payouts slots
Hey! I just wanted to ask if you ever have any issues with
hackers? My last blog (wordpress) was hacked and I ended up losing a few
months of hard work due to no back up. Do you have any solutions
to prevent hackers?
Introduction to Inspired High-End Purses
Causes for an Fame of Inspired Designer Accessories
Inspired High-End Bags Hold Increased On Fame DueTo Various Persuasive Reasons
Price Represent Commonly An Motivating Influence
Excellent InspiredVersions Offer The Enticement From Designer Designers No An High Cost
Improvements With Manufacturing Mean Which These InspiredVersions Often Have Exceptional Likenesses To The Originals
For Individuals Which Value Design However Are Frugal
Imitation Premium Handbags Supply The Alluring Replacement
Suggestions To Spotting Validity
Examining Characteristics Including Emblems Joints Together Components
Might Assist Decide A Imitation’s Legitimacy
Comparing Those Elements To Images Of Actual Brand Accessories
Might Show Several
Alternative InPlaceOf Copy Premium Handbags
Reasonable Brand Replacements
Are Obtainable Which Provide Trendy Replacements WithNo GivingUp On High Standards
Those Products Represent Legally Built With Designers That Provide Luxury Vogue At Accessible
20 Insightful Quotes On Birth Injury Litigation Birth Injuries;
S40.Cubecl.Com,
It’s really a great and useful piece of info.
I’m happy that you shared this helpful info with us.
Please keep us up to date like this. Thank you
for sharing.
The Slot Reviews Awards: The Best, Worst, And
Most Bizarre Things We’ve Seen Top-Rated Online Slots
I know this if off topic but I’m looking into starting my own weblog and
was curious what all is needed to get setup? I’m assuming having a blog like yours would cost a pretty penny?
I’m not very web savvy so I’m not 100% certain. Any tips
or advice would be greatly appreciated. Thank you
Also visit my web page fun88
The Reasons To Work With This Penny Slots real Casino Slots –
http://www.ky58.cc –
5 Laws To Help In The Slot Mechanics Industry slot machines – Stephania,
Hmm it looks like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I wrote
and say, I’m thoroughly enjoying your blog. I too
am an aspiring blog blogger but I’m still new to everything.
Do you have any suggestions for rookie blog writers?
I’d definitely appreciate it.
Feel free to surf to my blog post :: اجاره خودرو در دبی
What Can A Weekly Online Shopping Figures Uk Project Can Change Your Life {Ticonderoga Laddie Tri-Write Pencils|Wood-Cased
Greetings from Florida! I’m bored to tears at work so I decided to browse your site on my iphone during lunch break. I really like the info you provide here and can’t wait to take a look when I get home. I’m amazed at how quick your blog loaded on my mobile .. I’m not even using WIFI, just 3G .. Anyhow, fantastic blog!
The 10 Scariest Things About Boat Accident Attorneys boat accident (Chauncey)
Introduction on Replica Upscale Luggage
Motives for an Popularity of Replica Designer Handbags
Copy High-End Purses Have Risen On Popularity BecauseOf Various Strong Reasons
Expense Symbolize Regularly A Influencing Reason
Superb Copies Present A Appeal About Label Brands Without A Hefty Expense
Improvements In Manufacturing Represent Which These InspiredVersions Commonly Possess Noticeable Likenesses About An Originals
For People That Enjoy Design However Be Economical
Inspired Upscale Bags Give An Attractive Option
Advice To Recognizing Genuineness
Analyzing Features Like Emblems Stitching Along Parts
Will Help Discover A Imitation’s Legitimacy
Differentiating Them Elements Against Visuals From Authentic Brand Purses
Will Expose Numerous
Alternatives InPlaceOf Inspired Upscale Baggage
Reasonable Brand Replacements
Represent Obtainable That Offer Fashionable Replacements No Sacrificing Upon Greatness
Those Products Be Genuinely Built With Labels Which Offer Upscale Design For Accessible
Your means of explaining all in this article is actually fastidious,
all can effortlessly know it, Thanks a lot https://lepetitdep.ca/products/copie-de-beurre-derable
With havin so much content do you ever run into any issues
of plagorism or copyright violation? My website has a lot of
exclusive content I’ve either authored myself or outsourced but it looks like a lot of it
is popping it up all over the internet without my authorization. Do you know any methods
to help reduce content from being stolen? I’d really appreciate it.
How Slot Developers Can Be Your Next Big Obsession Top Slots
Slot Design: 10 Things I’d Like To Have Learned Earlier top developer Slots
Howdy! Quick question that’s entirely off topic.
Do you know how to make your site mobile friendly?
My web site looks weird when viewing from my iphone4.
I’m trying to find a theme or plugin that might be able to
correct this issue. If you have any suggestions, please share.
Thanks!
Today, I went to the beach front with my kids.
I found a sea shell and gave it to my 4 year old
daughter and said “You can hear the ocean if you put this to your ear.” She put
the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is totally off topic but I had to tell someone!
Is Tech Making Slot Sound Effects Better Or Worse?
Slot Wins
I have to thank you for the efforts you’ve put in writing this
blog. I really hope to view the same high-grade content from you later on as
well. In truth, your creative writing abilities has encouraged me to get my very own site now 😉
Do Not Believe In These “Trends” About Shopping Online Sites Signature Collection Space Aliens 64018
Thanks in support of sharing such a good thought, post is
nice, thats why i have read it completely
Really tons of useful info!