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
https://finasteride.store/# buying propecia for sale
Αn outstanding share! I have just forwarded this onto a co-workeг who has been conducting a litle researdh on this.
Αnd he in fact orɗered me dinner because I stumbled upon it
for him… loⅼ. So let me reword this…. Thannks for the meal!!
Butt yeah, thanks for spending the tije to discuss this issue here on your weƄ ρage. https://able.extralifestudios.com/wiki/index.php/Rakyat_Hutan_Menyejahterakan_Rp_50._000_Dalam_Undian_Ck4d_Link_Alternatif_Ck4d_Login_Ohio
cipro pharmacy ciprofloxacin generic cipro ciprofloxacin
https://ciprofloxacin.tech/# buy ciprofloxacin
https://ciprofloxacin.tech/# ciprofloxacin
http://lisinopril.network/# cost for 40 mg lisinopril
http://cytotec.club/# buy cytotec pills
It is important to raise a Cockatoo as a member of the family unit and not as the center of it.
cheap propecia without dr prescription: cost of cheap propecia price – propecia pill
buy cytotec in usa buy cytotec over the counter п»їcytotec pills online
Diazepam belongs to a group of drugs called benzodiazepines.
http://ciprofloxacin.tech/# ciprofloxacin 500 mg tablet price
The 10 Most Scariest Things About Ticktok
Pornstars Ticktok Pornstars
cytotec pills buy online buy cytotec online fast delivery cytotec abortion pill
Guide To Method Titration: The Intermediate Guide Towards Method
Titration method titration
Покердом является официальным партнером Sochi Poker Festival.
Greetings! Very helpful advice in this particular article!
It is the little changes that make the most significant changes.
Many thanks for sharing!
https://cytotec.club/# Cytotec 200mcg price
What’s The Job Market For Adhd Adult Treatment
Professionals? Adhd Adult Treatment
http://lisinopril.network/# best generic lisinopril
http://nolvadex.life/# tamoxifen 20 mg
See What Examples Of Online Products Tricks The Celebs Are Making Use Of examples Of online Products
Hello my family member! I want to say that this post is awesome, great written and come with almost all important infos.
I’d like to look more posts like this .
Also visit my page Lahore Escorts
ciprofloxacin 500mg buy online cipro for sale cipro pharmacy
http://lisinopril.network/# lisinopril 10 best price
Hi there to all, since I am really eager of reading this
weblog’s post to be updated on a regular basis.
It consists of nice stuff.
Итак, за 1-й депозит от рублей пользователю выдают 100 percent бонус.
The Most Effective Assessments For Adhd Tricks To Make A Difference In Your
Life Adult
https://cytotec.club/# buy cytotec over the counter
zestoretic 20 generic lisinopril online lisinopril without rx