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
enclosed car shipping enclosed car shipping .
The 10 Most Scariest Things About Upvc Doors Repair Upvc Doors Repair
reputable mexican pharmacies online: reputable mexican pharmacies online – buying prescription drugs in mexico online
mexico drug stores pharmacies: purple pharmacy mexico price list – reputable mexican pharmacies online
mexican mail order pharmacies: medicine in mexico pharmacies – mexico drug stores pharmacies
What Is The Window Doctors Term And How To Utilize It
the Window doctors
This Story Behind Anxiety Symptoms In Women Will Haunt You For The Rest Of Your Life!
How reduce anxiety symptoms (https://historydb.date)
You Will Meet The Steve Jobs Of The Truck Accident Industry truck Accident lawsuit (rossi-refsgaard-2.federatedjournals.Com)
7 Secrets About Slot Demo Gratis Pragmatic Play No Deposit That Nobody Can Tell You wild west gold demo
20 Things You Must Be Educated About Treadmill With Incline Uk Incline Treadmill Argos;
https://Www.Valeriarp.Com.Tr/Index.Php?Action=Profile;U=56759,
Nine Things That Your Parent Teach You About Shop Online And Compare Prices shop online and compare Prices
24 Hours For Improving L Shaped Sofa Small grey l Shaped sofa
mexico drug stores pharmacies reputable mexican pharmacies online purple pharmacy mexico price list
buying from online mexican pharmacy buying from online mexican pharmacy best online pharmacies in mexico
Who Is Responsible For A 18 Wheeler Wreck Lawyer Budget?
12 Ways To Spend Your Money 18 wheeler Accidents
buying prescription drugs in mexico: п»їbest mexican online pharmacies – mexican drugstore online
A The Complete Guide To Sweet Bonanza Hari Ini From Start To Finish
sweet fiesta slot
buying from online mexican pharmacy mexico pharmacies prescription drugs mexico pharmacy
7 Essential Tips For Making The Greatest Use Of Your Seat Key Fob Replacement Seat Leon Key Programming
medicine in mexico pharmacies п»їbest mexican online pharmacies best online pharmacies in mexico
10 Sites To Help Be A Pro In Bagless Electric Robots Best Bagless Robot Vacuum
buying prescription drugs in mexico online: mexican pharmaceuticals online – mexican rx online
reputable mexican pharmacies online mexico drug stores pharmacies mexican mail order pharmacies
Why People Don’t Care About Starlight Princess Max Win demo princess Starlight pragmatic
enclosed vehicle transport company https://www.Autoshipping24.com .
car shipping companies enclosed Autoshipping24.com .
closed carrier car transport https://www.Autoshipping24.com .
Five Killer Quora Answers To Sectional With Pull Out
Ottoman sectional with pull out ottoman
best online pharmacies in mexico: mexico pharmacies prescription drugs – best online pharmacies in mexico
Why We Why We ADD And Treatment (And You Should Also!) adhd treatment reddit – http://Www.cheaperseeker.Com –