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
15 Top Pinterest Boards From All Time About Volkswagen Key Programming
volkswagen Key Copy
На украинский рынок это интернет https://1win-pas-official2.top/ вышло в 2021 году.
See What Best Bunk Bed Brands Tricks The Celebs Are Making Use Of best bunk bed Brands
Wow! In the end I got a web site from where I
be able to genuinely take valuable information concerning my study and knowledge.
https://www.kabinetginecologa.ru/
The Unspoken Secrets Of Volkswagen Key Replacement Volkswagen key maker
I’m really enjoying the theme/design of your blog.
Do you ever run into any internet browser compatibility problems?
A number of my blog visitors have complained about my
site not operating correctly in Explorer but looks great in Firefox.
Do you have any suggestions to help fix this problem?
Just wish to say your article is as astounding. The clearness to your submit is simply great and i can assume you are knowledgeable in this subject.
Fine along with your permission let me to grab your RSS feed to keep
updated with forthcoming post. Thank you 1,000,000 and please continue the gratifying work.
5 Lessons You Can Learn From Best Value Bean To Cup Coffee Machine best bean to cup coffee beans (https://www.valeriarp.com.Tr/index.php?action=profile;u=231275)
9 Things Your Parents Teach You About Repairing Bottom Of Bifold Door Repairing Bottom Of Bifold Door (Medik.Co.Kr)
10 Things That Your Family Teach You About Daftar Akun Togel Resmi daftar akun Togel Resmi
10 Wrong Answers To Common Robot Vacuum And Mop Pet
Hair Questions Do You Know The Right Ones? best robot vacuum and mop for pet
hair; http://Www.otyjob.com,
9 Signs You’re A Robot Vacuums For Pet Hair Expert Best Robot Vacuum For Large House
You actually make it seem so easy with your presentation but I find
this matter to be really something that I think I would never understand.
It seems too complicated and very broad for me. I’m looking forward for your
next post, I’ll try to get the hang of it!
Here is my blog post provadent doctor
Hello There. I found your blog using msn. This is a very well written article.
I will make sure to bookmark it and return to read more
of your useful information. Thanks for the post. I’ll definitely comeback.
Here is my page; zwoltek01
slot siteleri: deneme bonusu veren siteler – oyun siteleri slot
The 10 Most Terrifying Things About 3 Seater Chaise 3 Seater chaise, https://www.metasoa.com/,
The Reasons To Focus On Improving ADD Treatment For Adults severe untreated Adhd in adults
выведение из запоя нижний новгород выведение из запоя нижний новгород .
капельница от запоя на дому нижний новгород капельница от запоя на дому нижний новгород .
Hello there, I do think your site could be having browser compatibility issues.
When I look at your site in Safari, it looks fine
however when opening in I.E., it has some overlapping issues.
I merely wanted to give you a quick heads up! Apart from that, excellent website!
I’m not sure why but this blog is loading extremely slow for me.
Is anyone else having this issue or is it a problem on my end?
I’ll check back later on and see if the problem still exists.
Greetings! I know this is kinda off topic but I was wondering which blog platform are you using for this website?
I’m getting tired of WordPress because I’ve had problems
with hackers and I’m looking at alternatives for another platform.
I would be fantastic if you could point me in the direction of a good
platform.
my web page … does tonic greens cure hsv2
http://denemebonusuverensiteler.win/# bahis siteleri
Do Not Buy Into These “Trends” Concerning Car Locksmiths Near Me mobile
Guide To Best Car Seat For Newborn: The Intermediate Guide
To Best Car Seat For Newborn Best car Seat for newborn
You’ll Be Unable To Guess Long Couch With Chaise’s Tricks Couch With Chaise
https://sweetbonanza.network/# sweet bonanza guncel
The Reasons Self Empty Robot Vacuum And Mop Is Everywhere This Year best robot vacuum and mop self cleaning
slot kumar siteleri: slot kumar siteleri – deneme veren slot siteleri