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
http://nolvadex.life/# clomid nolvadex
І’m impressed, I һave to admit. Seldom do I cme acfross ɑ blog that’s equally educative and engaging, ɑnd let me tell you, yoou have
hit tһе nail օn the head. Tһe issue іs somethin not
enough men ɑnd women aгe speaking intelligently ɑbout.
I’m very happʏ that I cаme across thius in my search fօr something
concеrning tһis.
Review my web page uutisia internetistä tänään
https://nolvadex.life/# nolvadex steroids
https://cytotec.club/# cytotec buy online usa
generic propecia online order cheap propecia buying generic propecia tablets
All-Inclusive Guide To Irobot Vacuum robot vacuum for stairs (morgan-foged-4.mdwrite.net)
https://cytotec.club/# buy cytotec online fast delivery
tamoxifen pill nolvadex gynecomastia nolvadex gynecomastia
http://cytotec.club/# buy cytotec
13 Things About Test For Adult ADHD You May Not Have Considered adult testing adhd
7 Tricks To Help Make The Greatest Use Of Your
Diagnostic Check Near Me diagnostic car (Mathias)
propecia pills cost of propecia without rx order cheap propecia without insurance
A Productive Rant Concerning Uk Online Shopping Sites For
Electronics Charlotte Pipe 4 Vent Tee
lisinopril metoprolol: lisinopril 20 mg generic – lisinopril price 10 mg
10-Pinterest Accounts You Should Follow About Bean To Cup Coffee Machine
bean-to-cup coffee machines
buy cipro where can i buy cipro online purchase cipro
10 Things That Your Family Taught You About Bmw Replacement Car Keys bmw Replacement car keys
You’ll Never Guess This Best Robot Vacuum Self
Emptying’s Secrets best robot vacuum Self empty
Where Can You Get The Most Effective Ford Key Cutting Information? Ford Transit Key Replacement
lisinopril 30 mg cost: lisinopril generic brand – lisinopril 20 mg uk
buying generic propecia tablets: propecia pill – get generic propecia prices
A List Of Common Errors That People Make With Repairs To
Upvc Windows double glazing repairs (Latonya)
lisinopril 5mg buy zestoretic online lisinopril 20 pills
https://cytotec.club/# buy cytotec in usa
Audi A3 Car Key Battery: The Ultimate Guide To Audi
A3 Car Key Battery Replacement car keys Audi
Guide To Robot Vacuum And Mop With Self Emptying: The Intermediate Guide For Robot Vacuum And Mop
With Self Emptying Robot Vacuum And Mop With Self Empty
prednisone buying
Ten Situations In Which You’ll Want To Learn About Ghost Installations audi rs3 ghost installer
(https://grape-heron-fvmvcc.mystrikingly.com/blog/Be-on-the-lookout-for-how-autowatch-ghost-installers-is-gaining-ground-and)
is nolvadex legal: buy nolvadex online – how to lose weight on tamoxifen
This is my first time go to see at here and i am actually pleassant to read all at single
place.