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
top 10 pharmacies in india world pharmacy india pharmacy website india
Situs Sabung Ayam Digmaan
Become A Avon Representative Strategies That Will Change Your Life
customer
The Next Big New Bmw Spare Key Cost Industry Bmw Spare Key
7 Simple Tips To Totally Rocking Your Replacement Key For Audi audi q7 Replacement key
5 Laws Anyone Working In Top Table Freezer Should Know Large Freezers; https://Minecraftathome.Com/Minecrafthome/Show_User.Php?Userid=18281127,
The Biggest Problem With Motor Vehicle Law, And How You Can Fix It
Motor Vehicle Accident lawyers
The 10 Most Scariest Things About Veleco Mobility Scooters veleco
15 Bizarre Hobbies That’ll Make You Smarter At Audi A4 Key Replacement car key cover audi
7 Essential Tips For Making The Most Of Your Audi A1 Key Replacement audi a1 Key replacement
mexican border pharmacies shipping to usa Mexican Pharmacy Online medicine in mexico pharmacies
What Is The Evolution Of Double Glazed Window Replacement Near Me Double glazed window Locks
Credit and bank cards (visa and mastercard): The process of
withdrawal finance can take from 1 to seven working
days, what meets standard banking procedures online casino Betwinner.
Also visit my blog :: Betwinner top bets
mexican border pharmacies shipping to usa Online Pharmacies in Mexico mexican border pharmacies shipping to usa
15 Documentaries That Are Best About CSGO Cases Opening shattered Web case
Undeniably believe that which you said. Your favorite reason seemed to be on the net the simplest thing to
be aware of. I say to you, I definitely get irked while people consider worries that they plainly don’t know about.
You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people
can take a signal. Will likely be back to get more. Thanks
Expert Advice On 18 Wheeler Wreck Lawyer From A
Five-Year-Old 18 Wheeler Accidents
Do You Think What Cases Are Being Dropped CSGO Ever Be The
King Of The World? revolution case [https://salinas-Eaton-2.mdwrite.net]
indian pharmacy online buy medicines from India online shopping pharmacy india
reputable mexican pharmacies online mexico pharmacies prescription drugs buying prescription drugs in mexico
How To Save Money On Double Glazing Repairs Leeds door repairs leeds – https://bookmarking.win/story.php?title=find-out-what-double-glazed-window-Leeds-the-celebs-are-making-use-of,
What Is Avon Starter Kit 2023 Uk’s History? History Of Avon Starter Kit 2023 Uk
rep kit (https://site-9122459-8504-3671.mystrikingly.com/blog/these-ten-steps-will-avon-starter-kit-for-representatives-the-way-you-do)
http://mexicoph24.life/# mexican online pharmacies prescription drugs
Pay Attention: Watch Out For How Semi Truck Attorney Is Taking Over And What Can We Do About It semi truck accidents – Cortney,
Thank you a lot for sharing this with all of us you
really recognise what you are speaking about!
Bookmarked. Please also visit my web site =). We can have a hyperlink trade arrangement between us
How Cerebral Palsy Settlement Influenced My Life For The
Better cerebral Palsy Lawsuit
“The Ultimate Cheat Sheet” On Sofa Sets For
Sale modern Couches for Sale
Hey there would you mind letting me know which webhost
you’re working with? I’ve loaded your blog in 3
different web browsers and I must say this blog loads a lot faster then most.
Can you suggest a good web hosting provider at
a honest price? Thanks, I appreciate it!
Truly tons of useful information!
Ten Upvc Window Repairs That Really Change Your Life Upvc Window repairs