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 Is Appliances Online Beko Washing Machine And Why Is Everyone Speakin’ About
It?
prinivil 10 mg prinivil cost lisinopril 2.5 mg medicine
Stumbled upon interesting material – I can’t help but recommend you to read https://millioner.5bb.ru/viewtopic.php?id=7376#p22334
9 Things Your Parents Teach You About Injury Lawyer injury lawyer
Why We Do We Love Mesothelioma Lawsuits (And You Should, Too!) Asbestos Compensation
The Most Important Reasons That People Succeed In The Dangerous Drugs Lawyers Industry Dangerous Drugs Attorneys
10 Websites To Help You Learn To Be An Expert In Double Glazed Window
Repair Near Me near
tamoxifen for gynecomastia reviews tamoxifen and weight loss tamoxifen joint pain
buy cytotec online buy cytotec online fast delivery buy cytotec in usa
https://lisinopril.network/# lisinopril 12.5
Really plenty of fantastic information.
20 Insightful Quotes About Selling Avon Online Only direct selling avon (Dirk)
valtrex otc drug
http://finasteride.store/# get generic propecia without dr prescription
10 Things You’ve Learned About Preschool That’ll Help You Understand Online Shopping Uk Groceries
Husaberg oil filter
http://cytotec.club/# buy misoprostol over the counter
where to get nolvadex tamoxifen generic tamoxifen moa
How To Beat Your Boss On Double Glazing Showrooms Near
Me Double Glazing Door Lock (https://Qooh.Me)
https://lisinopril.network/# lisinopril 25 mg tablet
nolvadex 10mg nolvadex pills nolvadex for pct
10 Places That You Can Find Double Glazed Units Near Me
wood double glazing windows – Roseann –
It Is A Fact That What CSGO Cases Have Butterfly Knives Is The
Best Thing You Can Get. What CSGO Cases Have Butterfly Knives Spectrum 2 Case
Hello There. I found your blog using msn. This is a really well written article.
I will make sure to bookmark it and come back to read more of your useful information. Thanks for the post.
I’ll certainly return.
http://ciprofloxacin.tech/# buy ciprofloxacin
Boat Accident Case Tools To Ease Your Daily Lifethe One Boat Accident Case Technique Every Person Needs To Learn Accident
cheap propecia order cheap propecia prices buying generic propecia without prescription
ciprofloxacin: ciprofloxacin 500 mg tablet price – ciprofloxacin mail online
An outstanding share! I’ve just forwarded this onto a colleague who has been conducting a
little research on this. And he actually bought me dinner simply because I stumbled upon it for him…
lol. So let me reword this…. Thanks for the meal!!
But yeah, thanks for spending some time to discuss this topic here on your website.
Oh my goodness! Amazing article dude! Many thanks, However I am experiencing issues with your RSS.
I don’t understand why I cannot join it. Is there anyone else getting the same RSS issues?
Anyone that knows the answer can you kindly respond?
Thanx!!
https://cytotec.club/# purchase cytotec