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
What Do You Think? Heck What Exactly Is Designer Bags?
online designer handbags
Say “Yes” To These 5 Bunk Bed Online Store Tips Bunk Beds sale
You’ll Never Guess This Window Repair Near Me’s Secrets Window repair near me
The jackpot rose to $687 million with a money value of $332.three million.
My webpage … https://www.istitutomorgagni.it/the-ultimate-guide-to-winning-the-lottery-jackpot/
buy propecia pills: buy propecia no prescription – order propecia without prescription
Responsible For A Double Glazed Window Repairs Near Me Budget?
12 Top Notch Ways To Spend Your Money Double glazed wooden windows
https://nolvadex.life/# tamoxifen estrogen
You’ll Be Unable To Guess Railroad Injuries Settlement’s Secrets railroad injuries – http://m.042-527-9574.1004114.co.kr/,
It’s perfect time to make a ffew plans for thе lonng runn and it’s time
to be happy. I haᴠe read this pst and if I may just I wanmt to suggest yoou feew attention-grabbing issues or
tips. Perhuaps you coսld write next articles referring
to this ɑrtіcle. I desire to read even more issues approximately
it!
Feel free to visit myy sіte – ck4d link aⅼternatif – http://www.google.co.ck,
nolvadex only pct tamoxifen generic tamoxifen postmenopausal
tamoxifen effectiveness: natural alternatives to tamoxifen – who should take tamoxifen
The Largest Issue That Comes With Railroad Injuries Law, And
How You Can Repair It Railroad injuries lawyers
The 10 Scariest Things About Double Glazing Near Me Double Glazing Near Me
buy cytotec pills online cheap buy misoprostol over the counter п»їcytotec pills online
https://finasteride.store/# cost cheap propecia tablets
generic propecia without rx: cost cheap propecia without insurance – cost propecia online
https://ciprofloxacin.tech/# buy ciprofloxacin
Enough Already! 15 Things About Car Key Fob Replacement We’re Sick Of Hearing mobile car Key replacement Near me
http://lisinopril.network/# lisinopril medication otc
сериал
buy cipro online without prescription: ciprofloxacin generic – where can i buy cipro online
10 Strategies To Build Your Program A Car Key Empire how to program a car key
You’ll Never Guess This Link Daftar Gotogel’s Benefits
link daftar gotogel (Robert)
When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time
a comment is added I get several emails with the same comment.
Is there any way you can remove me from that service?
Thank you!
Tommy Hilfiger Bookbag Tips From The Top In The Business bag tommy hilfiger sale
http://lisinopril.network/# prinivil 5mg tablet
How To Explain Mesothelioma Claim To Your Grandparents Mesothelioma Law
I read this article fully on the topic of the comparison of latest and previous technologies, it’s amazing
article.
cipro for sale cipro purchase cipro
Who Is Window Repair Near And Why You Should Care window repair near me