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
See What Double Glazing Units Near Me Tricks The Celebs Are Using double glazing units near Me
Mobile vaporizers make use of batteries as a source of
power.
my blog – vegetable based vape juice
Both mushrooms products are immune contractors.
My webpage :: new mushroom head air cushion cc cream
reviews (Nestor)
XYGENYX, a licensing business for FDA-registered products.
my web site … how much is disability in ontario
11 Creative Ways To Write About Dangerous
Drugs Law Firm dangerous drugs law firms
Attractive section of content. I just stumbled upon your blog and in accession capital to assert that I acquire actually enjoyed account
your blog posts. Anyway I will be subscribing to your augment and even I achievement you access consistently quickly.
Feel free to surf to my webpage; the growth matrix torrent
can you get generic nemasole without a prescription
Hi there, I log on to your new stuff on a regular basis.
Your writing style is awesome, keep it up!
They have products to offer or services to provide.
Have a look at my webpage Bookmarks
17 Signs To Know You Work With Land Rover Key
land rover evoque key fob
A Relevant Rant About Semi Truck Lawsuits semi truck accident Lawsuit
You do NOT wish to consume Lion’s Which Lion'S Mane Is Best Mushrooms raw.
Portable vaporizers make use of batteries as a source of
power.
Here is my page Bookmarks
Hives redness
Allergic conjunctivitis treatment resources
Drug information. Drug Class.
buy cheap tetracycline tablets
All information about medicament. Get here.
Marvelous, what a weblog it is! This website presents useful data to us,
keep it up.
20 Things That Only The Most Devoted Slot Demo Zeus Vs Hades Fans Understand Erik
This website certainly has all of the information How Much Should I Pay For Seo Services needed concerning this subject and didn?t
know who to ask.
New 10kg Washing Machine: A Simple Definition 023456789
A synthetic USP human development hormonal agent (somatropin).
Visit my web page – Bookmarks
Nice weblog here! Also your web site rather a lot up fast!
What web host are you using? Can I am getting your affiliate link to your host?
I want my web site loaded up as fast as yours lol
Top 10 Online Shopping Sites In Uk For Clothes Tools To Ease Your Daily Lifethe One Top
10 Online Shopping Sites In Uk For Clothes Trick Every Person Should
Know top 10 online shopping sites in uk for clothes
CBD gummies claims to deal with the benefits of CBD.
Here is my web site: Bookmarks (Barbra)
I like reading a post that will make men and women think.
Also, many thanks for allowing me to comment!
my web-site :: Bookmarks
Medicine information sheet. Long-Term Effects.
where can i get atomoxetine without dr prescription
Everything trends of medicament. Read information here.
Finding out a offline language learning app reddit does not have to
be pricey.
Beware Of These “Trends” About Mini Sectional Sofa Tom
What Is Adhd Symptoms For Females And Why You Should Care Combined Adhd Symptoms
Medicines information for patients. Short-Term Effects.
can you get synthroid no prescription
Some trends of medication. Get now.