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
10 Methods To Build Your Window Repair Leeds Empire Door repairs leeds
http://kamagra.win/# Kamagra tablets
14 Clever Ways To Spend Leftover Double Glazed Window Replacement Near Me Budget double glazing panels
http://kamagra.win/# buy kamagra online usa
http://levitrav.store/# Cheap Levitra online
Three Reasons To Identify Why Your Cheap Couches For Sale Isn’t Working
(And How To Fix It) Sleeper Sofas for sale
http://kamagra.win/# buy kamagra online usa
9 . What Your Parents Teach You About Titration ADHD Medications Titration Adhd Medications
10 Things Everybody Hates About Marc Jacobs Crossbody
Bag marc jacobs tote bag sale, Lavon,
5 Laws That Will Help The Robot Vacuum And Mop Industry top 10 Robot vacuums
The Story Behind Bunk Beds Can Haunt You Forever! farmhouse Bunk Beds
fantastic publish, very informative. I ponder why the
opposite experts of this sector don’t notice this. You should continue
your writing. I’m confident, you have a huge readers’ base already!
Also visit my blog; Energy Efficient Lighting
Five Killer Quora Answers On Double Glazing Repairs Leeds double glazing repairs leeds
15 Top Online Shopping Sites Bloggers You Should Follow Integrated Led Fan
The Reason Everyone Is Talking About Double Glazed Window Repair Today Window Repairs Near Me
https://cialist.pro/# Tadalafil Tablet
Ten Coffee Bean Machines That Really Change Your Life Coffee Bean Machines
Ten Apps To Help Control Your Repair Double Glazing upvc Door repairs near me
https://cialist.pro/# Buy Tadalafil 10mg
cheapest cenforce: Cenforce 100mg tablets for sale – cenforce.pro
Why Double Glazing Repair Kit Isn’t A Topic That People Are Interested In. Window Doctor Near Me
7 Practical Tips For Making The Most Of Your Three Wheel Mobility Scooter 3 wheeled mobility scooters
Nine Things That Your Parent Taught You About
Large Sectional Sofa Sectional Sofa
https://levitrav.store/# Buy Vardenafil 20mg
9 Things Your Parents Teach You About Fiat Panda Key Fob Replacement fiat Panda key
Guide To Cleaning Robot Mop And Vacuum: The Intermediate Guide On Cleaning Robot Mop And Vacuum cleaning robot mop and vacuum
Kamagra Oral Jelly: kamagra.win – Kamagra 100mg
9 . What Your Parents Teach You About Mazda 2 Spare Key mazda 2 Spare Key
cenforce.pro: cheapest cenforce – Buy Cenforce 100mg Online
http://levitrav.store/# Buy Vardenafil 20mg