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
Replacement Audi Key 10 Things I Wish I’d Known Sooner audi replacement key Fob
Kamagra Oral Jelly: kamagra – cheap kamagra
Buy Cialis online Generic Cialis without a doctor prescription Cialis without a doctor prescription
How Amazon Online Grocery Shopping Uk Was The Most Talked About Trend In 2023 Offroad Lighting Solutions
The Three Greatest Moments In Which CS GO Case Is
Best To Open History shadow case (Katrice)
Why All The Fuss? Mesothelioma Attorney? mesothelioma legal
Cenforce 150 mg online: Buy Cenforce 100mg Online – cheapest cenforce
10 Healthy Semi Truck Settlement Habits Vimeo.Com
15 Terms Everybody In The Case Opening Sites CSGO Industry Should Know open cs2 Cases
buy Levitra over the counter Buy generic Levitra online Buy Vardenafil online
See What Multi Fuel Stoves For Sale Near Me Tricks The Celebs Are Utilizing multi fuel stoves for sale
Tadalafil price: buy cialis online – Buy Tadalafil 20mg
buy Viagra online: Buy Viagra online cheap – over the counter sildenafil
What’s Holding Back From The Adhd Treatment Without Medication Industry?
Near Me
Solutions To The Problems Of Double Glazing Repair Near Me double Glazing near me
http://levitrav.store/# Levitra 10 mg buy online
12 Stats About Erb’s Palsy Attorney To Make You Think About The Other
People howard Erb’s Palsy Lawyer
cenforce.pro: cheapest cenforce – Buy Cenforce 100mg Online
Why You’ll Want To Learn More About Renault Clio Key renault clio key fob, http://www.annunciogratis.net,
cheap kamagra: kamagra – Kamagra tablets
A Productive Rant About CSGO Weapon Case esports 2014
summer case (Fredrick)
Generic Tadalafil 20mg price Cialis 20mg price in USA Buy Tadalafil 20mg
Buy Tadalafil 10mg п»їcialis generic Generic Cialis price
30 Inspirational Quotes On Upvc Windows Repair Upvc Repair
Cialis over the counter: cialist.pro – buy cialis pill
What Is Asbestos Compensation? To Utilize It Asbestos Litigation
10 Things Everyone Hates About Replace Window Handle Replacement window Handle
I waas able tto find good information from your content.
Check out my blog post … Praca We WłOszech Dla Par
Levitra 10 mg best price: Cheap Levitra online – Levitra price
Cheapest Sildenafil online Cheap Viagra 100mg cheap viagra