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
deneme bonusu veren siteler betturkey betturkey.com: deneme bonusu veren siteler betturkey – deneme bonusu veren siteler denemebonusu2026.com
10 Quick Tips About Fire Suites electric fire suites sale (https://blogfreely.net/gardendriver89/why-Firesuites-should-be-your-next-big-obsession)
deneme bonusu veren siteler betturkey betturkey.com deneme bonusu veren siteler 2024 deneme bonusu veren siteler mycbet.com
After looking over a number of the blog articles on your blog, I really appreciate
your way of writing a blog. I saved it to my bookmark webpage list
and will be checking back soon. Please visit my web site as
well and let me know your opinion.
https://w2.angkasetan.uk/
deneme bonusu veren siteler denemebonusu2026.com deneme bonusu veren siteler deneme bonusu veren siteler betturkey
7 Essential Tips For Making The Maximum Use Of Your Private ADHD Assessment Near Me Private Assessment For Adhd Cost
az parayla cok kazandiran slot oyunlar? az parayla cok kazandiran slot oyunlar? en kazancl? slot oyunlar?
deneme bonusu veren siteler 2024 http://casinositeleri.win/# guvenilir casino siteleri
Тут можно преобрести сейф оружейный купить в москве сейф оружейный
The Biggest Problem With Assessing Adhd In Adults And How To Fix It
adhd Assessment Scotland
Покупка диплома о среднем полном образовании: как избежать мошенничества?
Heya! I just wanted to ask if you ever have any trouble with
hackers? My last blog (wordpress) was hacked and I ended up
losing a few months of hard work due to no back up. Do you
have any methods to protect against hackers?
I constantly spent my half an hour to read this blog’s articles or reviews every day along with a cup
of coffee.
I loved as much as you will receive carried out right here.
The sketch is tasteful, your authored material stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following.
unwell unquestionably come more formerly again as exactly the same
nearly very often inside case you shield this increase.
Five Killer Quora Answers To Fabric Corner Sofa Beige Fabric corner sofa
denemebonusuverensiteler.top: denemebonusuverensiteler.top – deneme bonusu veren siteler yerliarama.org
Тут можно преобрести пистолетные сейфы москва купить оружейный сейф
can i order generic synthroid without a prescription
guvenilir casino siteleri: Canl? Casino Siteleri – Deneme Bonusu Veren Siteler
Where Will Virtual Mystery Boxes Be 1 Year From In The Near Future?
online mystery box sites (Antonetta)
This Is A Guide To Prams 2 In 1 In 2023 2in1 pushchair
(linkagogo.trade)
Тут можно преобрести оружейные сейфы купить сейф для карабина
Oh my goodness! Amazing article dude! Thanks, However I am going through troubles with your RSS.
I don’t know the reason why I am unable to join it.
Is there anybody else getting the same RSS issues?
Anyone who knows the solution will you kindly respond?
Thanks!!
ultrabet bonus ultrabet ultrabet bonus
Всё о покупке аттестата о среднем образовании: полезные советы
deneme bonusu veren siteler yerliarama.org https://casinositeleri.win/# Casino Siteleri
Three Greatest Moments In Coffee Machine History best home coffee maker (Latosha)
Тут можно преобрести оружейный ящик купить пистолетные сейфы москва
9 Lessons Your Parents Teach You About Double Glazing Repair Near Me double glazing repair near me
– Jason,
15 Best 3 Wheel Travel System Bloggers You
Must Follow best 3 Wheel travel system