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
india pharmacy https://indiaph24.store/# best online pharmacy india
top 10 online pharmacy in india
mexican drugstore online: mexico pharmacy – medication from mexico pharmacy
Thanks , I have recently been searching for info about this topic for ages and yours is the best I have found out till now.
But, what concerning the conclusion? Are you certain in regards to the supply?
canada pharmacy online Large Selection of Medications from Canada safe canadian pharmacy
11 Strategies To Completely Redesign Your 12kg
Washing Machines washing Machine 12Kg price
What’s The Current Job Market For Injury Attorney Professionals Like?
attorney
http://mexicoph24.life/# pharmacies in mexico that ship to usa
is canadian pharmacy legit best online canadian pharmacy legal canadian pharmacy online
canada pharmacy canadian compounding pharmacy canadian pharmacy 24h com
Ten Treatments Of Adhds That Really Change Your Life
treatment for Adhd in adults uk
Speak “Yes” To These 5 Car Boot Lock Repair Tips central locking Repair near me
india pharmacy http://indiaph24.store/# mail order pharmacy india
top 10 online pharmacy in india
Why Window Companies Leeds Is Your Next Big Obsession upvc door repair meanwood
mexican drugstore online mexican pharmacy medication from mexico pharmacy
Leeds Window Repair Explained In Less Than 140 Characters upvc door repair
roundhay – Gaye,
10 Places To Find Which Online Stores Ship Internationally Syncpen For Tablets
cheapest online pharmacy india
5 Killer Quora Answers On Free Casino Slots free Casino Slots
Is Technology Making Mini Key Fob Replacement Better Or Worse?
Replace mini key fob
Seven Explanations On Why 12kg Washing Machines Uk Is Important
12kg washing Machines uk
What Is Lock Smith Car And Why Is Everyone Talking About It?
mobile Locksmith car keys
Ten Things You’ve Learned In Kindergarden To Help You
Get Started With Online Work And Earn Money Side Jobs From Home Online
Nine Things That Your Parent Taught You About Upvc Window Repairs Near Me upvc window Repairs Near me
http://canadaph24.pro/# certified canadian pharmacy
Word Rewriter Tools To Help You Manage Your Everyday Life
article rewriter ai [Margene]
You’ll Never Guess This Renault Master Key Programming’s Benefits
renault master key programming (Dulcie)
http://canadaph24.pro/# canadian drug
canada cialis 20mg
buy canadian drugs Licensed Canadian Pharmacy online canadian pharmacy
cheapest online pharmacy india: pharmacy website india – indian pharmacies safe