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://nolvadex.life/# who should take tamoxifen
3 Common Causes For Why Your Robot Vacuum Isn’t Working (And How To Fix It) Best Robot Vacuum For Pet Hair
15 Shocking Facts About Commercial Coffee Machines Best Value Bean To Cup Coffee Machine
You’ve Forgotten Window Glass Repairs: 10 Reasons Why
You Don’t Really Need It sash
ciprofloxacin order online: buy cipro online – ciprofloxacin mail online
A Look Inside Replacing Volkswagen Key’s Secrets Of Replacing
Volkswagen Key Costs
Aw, this was a very nice post. Spending some time and actual effort to
generate a really good article… but what can I say… I put things off a whole lot
and never seem to get nearly anything done.
Get Rid Of Avon Books Online: 10 Reasons That You No Longer Need It
Avon Book March 2023
We are a group of volunteers and opening a new scheme
in our community. Your site provided us with valuable info to work on. You’ve done a formidable job and our whole community will be thankful to you.
10 Things You Learned In Preschool That’ll Help You Understand
Double Glazing Near Me Replace double Glazed Window
http://lisinopril.network/# can i buy lisinopril in mexico
See What Dreametech L30 Ultra Tricks The Celebs Are
Utilizing Dreametech L30 (https://Upright-Elephant-H5Qqtz.Mystrikingly.Com)
generic propecia online cost of cheap propecia cost of cheap propecia price
http://finasteride.store/# order propecia without a prescription
После подтверждения от казино
вход в личный кабинет открыт.
This Week’s Most Popular Stories Concerning Mobility Scooters
Shops Near Me Mobility Scooters near me
cost cheap propecia without insurance: cheap propecia for sale – generic propecia price
Sex Machine In My Area Tools To Help You Manage Your Everyday Lifethe Only Sex Machine In My Area
Trick That Everybody Should Know sex Machine in my Area
Your Family Will Thank You For Having This Replacement Double Glazed Glass Only Near Me replacing double Glazed glass
cost of cheap propecia tablets buying propecia online cost propecia without prescription
A Trip Back In Time What People Said About Vauxhall Corsa Replacement Key 20 Years Ago replacement car key vauxhall, Lavina,
Auto Door Lock Repair 10 Things I’d Like To Have Known In The Past key lock repair near me
https://ciprofloxacin.tech/# cipro for sale
lisinopril 20 mg online lisinopril 12.5 tablet lisinopril 10 mg price
9 . What Your Parents Taught You About Best Robot Vacuum And Mop Self Empty Best robot vacuum and mop self empty
The Most Worst Nightmare About Counter Strike
Online Come To Life kilowatt Case
Five Killer Quora Answers To Key Cuts For Cars key Cuts for Cars
http://lisinopril.network/# zestril cost
Все транзакции зашифрованы, и ваша личная
информация остается конфиденциальной.
5 Laws That Can Help The Double Glazing Windows Repairs Industry Window Repair