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 12 Best Door Fitters Bedford Accounts To Follow
On Twitter Bifold Doors Bedfordshire
The 10 Most Scariest Things About Fela Railroad Settlements fela railroad settlements
Best Online Shopping Sites For Clothes Tools To Make Your Everyday Lifethe
Only Best Online Shopping Sites For Clothes Technique Every Person Needs To Know best online shopping sites for Clothes (s40.cubecl.com)
I think the admin of this site is actually working hard for his web site,
because here every information is quality based information.
You’ll Never Be Able To Figure Out This Treadmills Home Gym’s Benefits treadmills home gym
I don’t even know how I ended up here, but I thought this
post was great. I do not know who you are but certainly you
are going to a famous blogger if you are not already 😉 Cheers!
Have you lost money on your QIWI wallet?
We know how stressful that can be.
No need to worry—we specializes in recovering missing money from QIWI wallets.
Having an experienced team, we’re confident we can provide assistance.
Reach out to us and let’s start the process of recovering your funds.
The Motive Behind Truck Accident Attorney Is The Most Sought-After Topic In 2022 truck accident Law firms (https://www.Carnrich.com/free/121703)
Ten Things You Need To Learn About What CSGO Cases Have Butterfly Knives spectrum case (quebec-quebec.net)
Do you have a spam issue on this blog; I also am
a blogger, and I was wanting to know your situation; we
have created some nice practices and we are looking to swap strategies with other folks, please shoot
me an email if interested.
The Often Unknown Benefits Of Bmw Spare Key Replacement Key For Bmw
Why Locksmith Near Me For Cars Is Relevant 2023 in my area
cabergoline pharmacy
20 Things You Need To Be Educated About Medium Variance Slots
high variance Slots
You made the point!
Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Ie.
I’m not sure if this is a format issue or something
to do with browser compatibility but I thought I’d post to let you know.
The design and style look great though! Hope you get the problem fixed soon. Many
thanks
The https://wafflestoken.com/ supply will be approximately 1,000,000,000 waffles. Presentations of WAFERS connected and wrapped in other blockchains are not included in data.
Why Nobody Cares About Hyundai Keys car
10 Websites To Help You Become An Expert In CSGO Most Profitable Cases
operation wildfire case – Miranda,
I really love your blog.. Excellent colors & theme.
Did you develop this amazing site yourself?
Please reply back as I’m wanting to create my very own website and would like to find out where you got this from or just what the theme is called.
Thanks!
These Are The Most Common Mistakes People Make With Online Slots slot symbols, Jess,
15 Gifts For The How Many Cases Are There In CSGO Lover In Your Life Cs2 Case
nuttige referentie
The 10 Most Terrifying Things About High Roller Slots high roller Slots
What Is Treadmills For Home UK And Why Is Everyone Talking About It?
offer
10 Tips For Double Glazing Installers Near Me That Are Unexpected Double glazing misting
Hey there great website! Does running a blog similar to this
take a massive amount work? I’ve absolutely no knowledge of coding but I had been hoping to
start my own blog in the near future. Anyhow, if you have
any suggestions or tips for new blog owners please share.
I know this is off subject but I just had to ask.
Cheers!
Stop by my website … does fitspresso really work
10 Websites To Help You Learn To Be An Expert In Upvc Doors Repair upvc doors
panels (https://trainbetty6.werite.net/)
Guide To Slot Wins: The Intermediate Guide On Slot Wins Slot Wins – Appc.Cctvdgrw.Com –
5 Where To Buy Electronics Online Lessons From Professionals
Drip System Pressure Reducer