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
lisinopril 80 mg cost of prinivil buy lisinopril online india
Hello! I could have sworn I’ve been to your blog
before but after going through some of the posts I realized it’s new to me.
Anyways, I’m certainly pleased I came across it and
I’ll be bookmarking it and checking back regularly!
The Reason Double Glazing Window Repair Is The Obsession Of Everyone In 2023 local
5 Killer Quora Answers On Designer Handbags Uk Sale
Designer Handbags Uk Sale
A Brief History Of The Evolution Of Bosch Side By Side Fridge Freezer american style fridge freezer side by
side (Vida)
Best Online Shopping Groceries Uk 10 Things I Wish I’d Known In The Past best online shopping sites in uk for Clothes
https://finasteride.store/# cost cheap propecia without dr prescription
How To Choose The Right Replacement Key For Car Online online
Mental Health: It’s Not As Difficult As You Think private Mental health assessment near me
lisinopril 2.5 cost generic prinivil lisinopril average cost
Could Window Repair Near Be The Answer For 2023’s Challenges?
window repair Near me
https://ciprofloxacin.tech/# purchase cipro
https://cytotec.club/# cytotec online
The Unspoken Secrets Of Diagnostics Automotive Diagnostic Check Engine
cipro generic: cipro – buy ciprofloxacin over the counter
If you are going for most excellent contents like I do, just go to see this web
page everyday for the reason that it provides feature contents, thanks
5. Fiat Replacement Key Projects For Any Budget Fiat van Key replacement
buying generic propecia without prescription: buy cheap propecia without a prescription – order generic propecia for sale
10 Locations Where You Can Find Best Robot Vacuum And Mop Best Robot Vacuum That Mops
How To Become A Prosperous Workers Compensation Settlement
Even If You’re Not Business-Savvy Workers’ Compensation
Coffee Machines Espresso Tools To Ease Your
Everyday Lifethe Only Coffee Machines Espresso Trick Every Person Should Be Able To Coffee machines Espresso
The No. Question That Everyone In Metal Anal Butt Plug Must Know How To Answer Remote Control Anal Butt Plug
buy cipro online without prescription buy cipro online purchase cipro
15 Latest Trends And Trends In Avon Login Representative
Login Avon login representative login [http://www.highclassps.com]
זה לוודא כי בוחרים לבחון את ההיצע השונה ובצורה טובה מספיק. כל אחד מכם שיעשה זאת יוכל להיות בטוח כי זו תהיה הדרך שלו להבטיח שהוא יוכל למצוא בעצם את שירותי הליווי הטובים ביותר שישנם היום. בין אם תמיד חשקתם בנערת ליווי אקזוטית או שמא מדובר בכל רעיון מיוחד נערות ליווי בתל אביב
The Advanced Guide To Designer Handbags Large designer bags (Bart)
12 Companies Setting The Standard In Planet Spa Avon Avon Planet Spa Sleep Spray
Nice blog! Is your theme custom made or did you download it from somewhere?
A theme like yours with a few simple tweeks would really make my blog shine.
Please let me know where you got your design. Cheers
24-Hours To Improve Double Glazed Windows Leeds patio door Repair leeds
Ten Replacement Keys For Cars That Really Improve
Your Life replacement keys for cars