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
Ten Brochure Avon UK That Will Change Your Life Brochure Avon
buy canadian drugs pharmacy in canada 77 canadian pharmacy
https://mexicoph24.life/# pharmacies in mexico that ship to usa
indian pharmacy paypal https://indiaph24.store/# indian pharmacy paypal
Online medicine home delivery
5 Killer Quora Answers To Leather Couches For Sale Leather Couch
You should be a part of a contest for one of the
greatest sites on the internet. I am going to recommend this blog!
Feel free to surf to my blog post … http://Orouo.com/index.php?a=stats&u=charlottegore75
How To Resolve Issues With Asbestos Case asbestos lawsuit (Bonny)
medicine in mexico pharmacies: cheapest mexico drugs – buying prescription drugs in mexico online
mail order pharmacy india Generic Medicine India to USA indian pharmacy paypal
15 Up-And-Coming Trucking Lawyers Bloggers You Need To Check Out truck accident law firm (Brayden)
п»їlegitimate online pharmacies india: buy medicines from India – india pharmacy
Wow! After all I got a website from where I be capable of in fact take helpful
information regarding my study and knowledge.
http://canadaph24.pro/# canadian pharmacies online
mexican drugstore online Online Pharmacies in Mexico best online pharmacies in mexico
canada rx pharmacy world Certified Canadian Pharmacies canadian pharmacy sarasota
Responsible For The Peugeot Key Fob Budget? 12 Best Ways To Spend Your Money peugeot 207 Car key
The 9 Things Your Parents Taught You About Cheap Online Shopping Sites Uk Cheap online shopping sites uk
9 Things Your Parents Teach You About Designer Handbags In Sale designer handbags In sale
online pharmacy canada canadian pharmacies canadian neighbor pharmacy
The 3 Biggest Disasters In Birth Injury Attorney The Birth Injury
Attorney’s 3 Biggest Disasters In History Birth Injury Lawsuits
(Vn.Easypanme.Com)
buy medicines online in india https://indiaph24.store/# cheapest online pharmacy india
pharmacy website india
https://mexicoph24.life/# mexico pharmacies prescription drugs
https://indiaph24.store/# online shopping pharmacy india
The other day, while I was at work, my sister stole
my iPad and tested to see if it can survive a 40 foot drop, just so she can be
a youtube sensation. My iPad is now broken and she has 83 views.
I know this is entirely off topic but I had to share it with someone!
What Is Avon Skin So Soft Dry Oil Spray Uses And Why You Should Consider Avon Skin So Soft Dry Oil Spray Uses avon skin so soft dry Oil spray uses
See What Window Repair Leeds Tricks The Celebs Are Using Window Repair Leeds (https://Rock8899.Com)
best online pharmacies in mexico cheapest mexico drugs mexico pharmacy
https://indiaph24.store/# online pharmacy india
5 Conspiracy Theories About Second Hand Folding
Mobility Scooters You Should Stay Clear Of Folding Mobility Scooter 23 stone
Thank you for every other excellent post. The place else
may just anybody get that type of information in such a perfect means
of writing? I’ve a presentation next week, and I’m at the look for such info.