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
en yeni slot siteleri: deneme bonusu veren siteler – oyun siteleri slot
The 10 Scariest Things About Retro Fridge Freezer Sale retro
fridge freezer sale – https://tongbet24.com,
Thanks for a marvelous posting! I really enjoyed reading it, you could be a great author.I will
be sure to bookmark your blog and may come back from now on. I want
to encourage you continue your great writing, have a nice
morning!
Good way of explaining, and good post to take facts on the topic of my presentation topic,
which i am going to deliver in academy.
You’ll Never Be Able To Figure Out This Is Treadmill
Incline Good’s Benefits is treadmill incline good (Adriana)
10 Things You Learned In Preschool That Can Help You In Twin Stroller With Car Seats double Pram buggy
http://slotsiteleri.bid/# slot kumar siteleri
Magnificent site. A lot of useful info here. I’m sending it to some friends ans also sharing in delicious.
And of course, thank you in your sweat!
5 Killer Quora Answers To Bifold Door Glass Replacement Cost Bifold Door Glass Replacement Cost
bonus veren slot siteleri: slot oyunlar? siteleri – yeni slot siteleri
Having read this I thought it was extremely enlightening.
I appreciate you finding the time and energy to put this article together.
I once again find myself spending a significant amount of time both reading and commenting.
But so what, it was still worthwhile!
It’s The One Single Beds Mattresses Trick Every Person Should Be Aware Of Mattresses Single Bed
5 Killer Quora Answers On Small Double Buggy Small Double Buggy
2024 en iyi slot siteleri: canl? slot siteleri – oyun siteleri slot
I’m curious to find out what blog system you have been using?
I’m experiencing some minor security problems with
my latest site and I would like to find something more safe.
Do you have any recommendations?
The Best Retro Fridge Freezer Black Tricks To Make A Difference
In Your Life best retro Fridge freezer
Sage Advice About Treadmill Incline From A Five-Year-Old
Small space treadmill with incline
If you wish for to obtain a great deal from this post then you have to apply such methods to your
won weblog.
https://denemebonusuverensiteler.win/# bonus veren siteler
طلایاب
You’re so interesting! I do not believe I’ve read a single thing like
that before. So good to discover another person with a few unique thoughts on this subject.
Seriously.. thank you for starting this up. This web site is something that’s needed on the web, someone with
a bit of originality!
Feel free to surf to my site: where can i buy tonic greens
It’s an amazing article for all the online users; they will
take benefit from it I am sure.
Guide To Situs 4d: The Intermediate Guide The Steps
To Situs 4d Situs 4D
10 Things You’ll Need To Know About Depression Symptoms And Anxiety Early Depression symptoms
5 Laws That’ll Help The Mazda 2 Spare Key Industry Mazda
2 Replacement Key (http://Led-5I8L419H33N.Net/Bbs/Board.Php?Bo_Table=0408&Wr_Id=141372)
10 Car Key Lamborghini-Related Projects To Stretch Your Creativity lamborghini smart key
The Electric Motorized Treadmill Case Study You’ll Never Forget motorized treadmills
14 Businesses Doing A Superb Job At Adult ADHD
In Women mild adhd In women – http://Hongseongilbo.Kr/,
15 Terms That Everyone Working In The Citroen Key Replacement Cost Industry Should
Know Replacement Citroen Key
yasal slot siteleri: canl? slot siteleri – casino slot siteleri