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
trouver un mГ©dicament en pharmacie: cialis prix – pharmacie en ligne avec ordonnance
https://tadalafilmeilleurprix.com/# acheter mГ©dicament en ligne sans ordonnance
pharmacie en ligne sans ordonnance
darknet links https://github.com/darknetmarketslist/darknetmarketslist onion dark website
Here is my blog post: Daycare Near Me – Find The Best Daycares Near You
п»їpharmacie en ligne france: Pharmacies en ligne certifiees – pharmacie en ligne france pas cher
Pharmacie en ligne livraison Europe http://tadalafilmeilleurprix.com/# pharmacie en ligne france livraison belgique
Land 3 bonus offer signs to activate the Reward Perk Game.
my site – online slots nz and online pokies nz
In this instance, the life insurance company’s money.
Also visit my blog post – ai detector for essays
Achat mГ©dicament en ligne fiable cialis sans ordonnance pharmacie en ligne avec ordonnance
Way cool! Some extremely valid points! I appreciate you penning this post plus
the rest of the site what is a sexual assault advocate
really good.
Allow Commercial truck Parts store near me show you in my
testimonial of NewULife.
In this case, the life insurance firm’s money.
Also visit my site … ai detector turnitin free online
Fantastic beat ! I wish to apprentice at the same time
as you amend your website, how can i subscribe for a weblog web site?
The account aided simple assault lawyers near me a appropriate deal.
I had been a little bit acquainted of this your broadcast offered brilliant transparent idea.
Somebody essentially help to make seriously posts I’d state.
That is the first time I frequented your web page and to this point?
I amazed with the analysis you made to make this particular post amazing.
Excellent job!
My website sexual assault lawyers philadelphia
The most effective CBD items are made from high-grade hemp cbd oil for cats.
darkmarket link https://github.com/darkmarkets2025we92r/darkmarkets2025 dark web drug marketplace
Authentic HGH Benefits (NewULife Hgh Gel Not Checked Yet).
my webpage nagad88 online casino Bangladesh login
An artificial USP human development hormone (somatropin).
My blog post :: miami corporate law
Thank you, I’ve just been searching for information approximately this
subject for ages and yours is the greatest I’ve found out so far.
However, what concerning the conclusion? Are you positive about the source?
Feel free to surf to my website sexual assault lawyers in charlotte nc